Sort

Sort

Utility class that provides methods to sort data from Arrays. These methods are intended to be used as compareFunctions by the standard JavaScript sort function.

Members

(static, constant) NUMERIC

Source:

Specifies to sort using a numeric comparison.

Methods

(static) Asc(properties, numericopt, substitutionopt) → {function}

Source:

Returns a sort function that will sort an object array in-place into ascending order using the given properties.

Example
// a becomes [{"prop": "1"}, {"prop": "2"}, {"prop": "10"}, {"prop": null}]
const a = [{"prop": "1"}, {"prop": null}, {"prop": "10"}, {"prop": "2"}];
a.sort(Sort.Asc("prop", Sort.NUMERIC, {"null": "9999"}));
Parameters:
Name Type Attributes Default Description
properties string | array

Object properties to use for the sort - to select a specific property from an array specify the index in the string e.g. 'property[0]' to use the first element.

numeric boolean <optional>
false

If true use a numeric (not string) comparison

substitution object <optional>

Allows values to be substituted with new ones before doing the compare. The substituted value is only used for the comparison; the original value remains in the sorted array.

Returns:
Type
function

(static) Desc(properties, numericopt, substitutionopt) → {function}

Source:

Returns a sort function that will sort an object array in-place into descending order using the given properties.

Example
// a becomes [{"prop": null}, {"prop": "10"}, {"prop": "2"}, {"prop": "1"}]
const a = [{"prop": "1"}, {"prop": null}, {"prop": "10"}, {"prop": "2"}];
a.sort(Sort.Desc("prop", Sort.NUMERIC, {"null": "9999"}));
Parameters:
Name Type Attributes Default Description
properties string | array

Object properties to use for the sort - to select a specific property from an array specify the index in the string e.g. 'property[0]' to use the first element.

numeric boolean <optional>
false

If true use a numeric (not string) comparison

substitution object <optional>

Allows values to be substituted with new ones before doing the compare. The substituted value is only used for the comparison; the original value remains in the sorted array.

Returns:
Type
function

(static) Specific(orig, property, desired) → {array}

Source:

Sort the given object array so that its values are in the same order as the desired order.

Example
// a becomes [{foo:"1"}, {foo:"2"}, {foo:"6"}, {foo:"5"}]
const a = Sort.Specific([{foo:"6"}, {foo:"5"}, {foo:"2"}, {foo:"1"}], "foo", ["1", "2"]);
Parameters:
Name Type Description
orig array

Original object array to be sorted - is not changed.

property string | null

Object property to order by. If set to null it will sort using the original's raw values. In this case original shoud be a simple array of scalar values.

desired array

Desired order of array elements. Any values in 'orig' that are not in 'desired' will be appended to the end of the result in their original order.

Returns:

Sorted array

Type
array