Version: v0.6.1 - Beta.  We welcome contributors & feedback.  THanks!


sort($functionOrMap = false)


Sort the order of items by value.

If functionOrMap is a function, it will be called with 2 arguments containing items. Return a numeric comparison (usually with the <=> operator) of the items to create custom sort behavior. (-1 = before, 0 = equal, 1 = after)

If functionOrMap is a Map, it can provide the following options:

    reverse: true|false,  // reverse order of sort

    type: (pick one)

        'regular'     // (default) do not change type of items
        'numeric'     // compare items as numbers
        'string'      // compare items as strings
        'natural'     // as 'string', w/natural number ordering
        'stringCase'  // case-sensitive version of 'string'
        'naturalCase' // case-sensitive version of 'natural'
$items = ['d', 'a', 'c', 'e', 'b'];

//= ['a', 'b', 'c', 'd', 'e']

// Reverse sort
$items.sort({ reverse: true });
//= ['e', 'd', 'c', 'b', 'a']

// With expression function
$items.sort(x{ $b <=> $a });
//= ['e', 'd', 'c', 'b', 'a']

// Case-sensitive sort (uppercase first)
$items = ['a1', 'A2', 'a3', 'A4'];
$items.sort({ type: 'stringCase' });
//= ['A2', 'A4', 'a1', 'a3]

// Natural sort (natural order of numbers)
$items = ['a2', 'a10', 'a1'];
$items.sort({ type: 'natural' });
//= ['a1', 'a2', 'a10']

// Regular sort (ascii order of numbers)
$items.sort({ type: 'regular' });
//= ['a1', 'a10', 'a2']

See Also