mOrder
mOrder
(compareFn: CompareFn) => (anArray: array) => anArray
Order an array using a compare function
Consider order instead.
This is for the uncommon case where mutatinganArray
is required.const arr = [3, 1, 2] const ascending = (left, right) => left - right const orderByAscending = mOrder(ascending) const mutatedArr = orderByAscending(arr) console.log(mutatedArr === arr) // is true console.log(arr) // is [1, 2, 3]
const arr = [3, 1, 2] const ascending = (left: number, right: number) => left - right const orderByAscending = mOrder(ascending) const mutatedArr = orderByAscending(arr) console.log(mutatedArr === arr) // is true console.log(arr) // is [1, 2, 3]
const arr = [3, 1, 2] const ascending = (left, right) => left - right const orderByAscending = mOrder(ascending) const mutatedArr = orderByAscending(arr) console.log(mutatedArr === arr) // is true console.log(arr) // is [1, 2, 3]
const arr = [3, 1, 2] const ascending = (left: number, right: number) => left - right const orderByAscending = mOrder(ascending) const mutatedArr = orderByAscending(arr) console.log(mutatedArr === arr) // is true console.log(arr) // is [1, 2, 3]
Sometimes we want to mutate an array by ordering it. 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 = mOrder(byWeightDesc) const orderAllByWeightDesc = forEach(orderByWeightDesc) orderAllByWeightDesc(inventory) console.log(inventory) // 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 = mOrder(byWeightDesc)<Item[]> const orderAllByWeightDesc = forEach(orderByWeightDesc)<Inventory> orderAllByWeightDesc(inventory) console.log(inventory) // 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 = mOrder(byWeightDesc) const orderAllByWeightDesc = forEach( orderByWeightDesc ) orderAllByWeightDesc(inventory) console.log(inventory) // 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 = mOrder(byWeightDesc)< Item[] > const orderAllByWeightDesc = forEach( orderByWeightDesc )<Inventory> orderAllByWeightDesc(inventory) console.log(inventory) // 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 } // ] // }