order

order

  • (compareFn: CompareFn) => (anArray: array) => array
  1. Order an array using a compare function

    const arr = [3, 1, 2]
    
    const ascending = (left, right) => left - right
    const orderByAscending = order(ascending)
    const orderedArr = orderByAscending(arr)
    
    console.log(orderedArr) // is [1, 2, 3]
    const arr = [3, 1, 2]
    
    const ascending = (left: number, right: number) => left - right
    const orderByAscending = order(ascending)
    const orderedArr = orderByAscending(arr)
    
    console.log(orderedArr) // is [1, 2, 3]
    
    const arr = [3, 1, 2]
    
    const ascending = (left, right) => left - right
    const orderByAscending = order(ascending)
    const orderedArr = orderByAscending(arr)
    
    console.log(orderedArr) // is [1, 2, 3]
    const arr = [3, 1, 2]
    
    const ascending = (left: number, right: number) =>
      left - right
    const orderByAscending = order(ascending)
    const orderedArr = orderByAscending(arr)
    
    console.log(orderedArr) // is [1, 2, 3]
    
  2. Sometimes we want to order an array. Below, we are building a game with an inventory. Players will want to see what's weighing them down, so let's allow them to order by weight.

    const inventory = {
      potions: [
        { name: 'heal', weight: 5 },
        { name: 'cloak', weight: 8 },
        { name: 'attack up', weight: 6 },
      ],
      weapons: [
        { name: 'sword', weight: 10 },
        { name: 'axe', weight: 30 },
        { name: 'wand', weight: 8 },
      ],
    }
    
    const byWeightDesc = compareByProp('weight', withNumbersDescending)
    const orderByWeightDesc = order(byWeightDesc)
    const orderAllByWeightDesc = mapValues(orderByWeightDesc)
    const orderedInventory = orderAllByWeightDesc(inventory)
    
    console.log(orderedInventory)
    // is {
    //   potions: [
    //     { name: cloak, weight: 8 }
    //     { name: attack up, weight: 6 }
    //     { name: heal, weight: 5 }
    //   ]
    //   weapons: [
    //     { name: axe, weight: 30 }
    //     { name: sword, weight: 10 }
    //     { name: wand, weight: 8 }
    //   ]
    // }
    type Item = {
      name: string
      weight: number
    }
    type Inventory = Record<string, Item[]>
    const inventory: Inventory = {
      potions: [
        { name: 'heal', weight: 5 },
        { name: 'cloak', weight: 8 },
        { name: 'attack up', weight: 6 },
      ],
      weapons: [
        { name: 'sword', weight: 10 },
        { name: 'axe', weight: 30 },
        { name: 'wand', weight: 8 },
      ],
    }
    
    const byWeightDesc = compareByProp('weight', withNumbersDescending)<Item>
    const orderByWeightDesc = order(byWeightDesc)<Item>
    const orderAllByWeightDesc = mapValues(orderByWeightDesc)<Inventory>
    const orderedInventory = orderAllByWeightDesc(inventory)
    
    console.log(orderedInventory)
    // is {
    //   potions: [
    //     { name: cloak, weight: 8 }
    //     { name: attack up, weight: 6 }
    //     { name: heal, weight: 5 }
    //   ]
    //   weapons: [
    //     { name: axe, weight: 30 }
    //     { name: sword, weight: 10 }
    //     { name: wand, weight: 8 }
    //   ]
    // }
    
    const inventory = {
      potions: [
        { name: 'heal', weight: 5 },
        { name: 'cloak', weight: 8 },
        { name: 'attack up', weight: 6 },
      ],
      weapons: [
        { name: 'sword', weight: 10 },
        { name: 'axe', weight: 30 },
        { name: 'wand', weight: 8 },
      ],
    }
    
    const byWeightDesc = compareByProp(
      'weight',
      withNumbersDescending
    )
    const orderByWeightDesc = order(byWeightDesc)
    const orderAllByWeightDesc = mapValues(
      orderByWeightDesc
    )
    const orderedInventory =
      orderAllByWeightDesc(inventory)
    
    console.log(orderedInventory)
    // is {
    //   potions: [
    //     { name: cloak, weight: 8 }
    //     { name: attack up, weight: 6 }
    //     { name: heal, weight: 5 }
    //   ]
    //   weapons: [
    //     { name: axe, weight: 30 }
    //     { name: sword, weight: 10 }
    //     { name: wand, weight: 8 }
    //   ]
    // }
    type Item = {
      name: string
      weight: number
    }
    type Inventory = Record<string, Item[]>
    const inventory: Inventory = {
      potions: [
        { name: 'heal', weight: 5 },
        { name: 'cloak', weight: 8 },
        { name: 'attack up', weight: 6 },
      ],
      weapons: [
        { name: 'sword', weight: 10 },
        { name: 'axe', weight: 30 },
        { name: 'wand', weight: 8 },
      ],
    }
    
    const byWeightDesc = compareByProp(
      'weight',
      withNumbersDescending
    )<Item>
    const orderByWeightDesc = order(
      byWeightDesc
    )<Item>
    const orderAllByWeightDesc = mapValues(
      orderByWeightDesc
    )<Inventory>
    const orderedInventory =
      orderAllByWeightDesc(inventory)
    
    console.log(orderedInventory)
    // is {
    //   potions: [
    //     { name: cloak, weight: 8 }
    //     { name: attack up, weight: 6 }
    //     { name: heal, weight: 5 }
    //   ]
    //   weapons: [
    //     { name: axe, weight: 30 }
    //     { name: sword, weight: 10 }
    //     { name: wand, weight: 8 }
    //   ]
    // }