HexagonJS
Edit Page
Sort
Utility functions for sorting data, including some support for localised sorting.
Examples
hx.sort.compare('a','b') // returns -1
hx.sort.compare('b','a') // returns 1
hx.sort.compare('a','a') // returns 0

array = ['b','c','a']

array.sort(hx.sort.compare) // returns ['a', 'b', 'c']

array = ['20','1','100']

array.sort() // returns ['1', '100', '20']
array.sort(hx.sort.compare) // returns ['1', '20', '100']
array = ['é', 'e', 'z', 'è', 'a', 'ä']

array.sort() // returns ['a', 'e', 'z', 'ä', 'è', 'é']
array.sort(hx.sort.localeCompare()) // returns ['a', 'ä', 'e', 'é', 'è', 'z']

array.sort(hx.sort.localeCompare('de')) // returns ['a', 'ä', 'e', 'é', 'è', 'z']
array.sort(hx.sort.localeCompare('sv')) // returns ['a', 'e', 'é', 'è', 'z', 'ä']
Api
Objects
hx.sort
An object containing different methods for sorting array values.
Properties
A function to compare two items and return a number based on the priority. It takes into account the character order of text and the numeric order of numbers, as opposed to a standard text sort.
array = ['20','1','100']
array.sort() // returns ['1', '100', '20']
array.sort(hx.sort.compare) // returns ['1', '20', '100']
Arguments
The first item to compare
The second item to compare
Returns
1 when b should be before a, -1 when a should be before b and 0 when the terms match.
compareNullsLastaString / NumberbString / NumberNumber
A sort function that sorts nulls to the end of an array.
Arguments
The first item to compare
The second item to compare
Returns
1 when b should be before a, -1 when a should be before b where defined < null < undefined and 0 when the terms match.
localeComparelocaleStringoptionsObjectFunction
A function to return a localised sort function to use directly when calling array.sort.
It takes into account the character order of text for a specific locale and the numeric order of numbers.
Some browsers don't support the locale and options parameters.
Browser support and accepted values for the locale and options parameters can be found on the
svCompare = hx.sort.localeCompare('sv')
array.sort(svCompare)
svCompare in the above example would evaluate to:
function(a, b){
  return hx.sort.localeCompare(a, b, 'sv')
};
Arguments
localeString
The locale to use when sorting
options
The options to use when sorting
Properties
nullsLastBoolean
Whether to sort nulls to the end of the array.
Default: false
Returns
A function that compares two strings based on the passed in locale and options. To be passed into Array.sort.
Arguments
The first item to compare
The second item to compare
Returns
1 when b should be before a, -1 when a should be before b and 0 when the terms match.
Functions
hx.sortarrArray[Any]Array[Any]
Sorts an array without modifying it
Arguments
arrArray[Any]
The array to sort
Returns
Array[Any]
The sorted array
hx.sortByarrArray[Any]fFunctionArray[Any]
Sorts an array without modifying it. The resultant array is sorted by the value obtained by calling f on each member of the array.
Arguments
arrArray[Any]
The array to sort
fitemAnyitemAny
A function that takes each element of an array and returns a value that can be compared using < and ==
Arguments
itemAny
The item to extract the value from
Returns
itemAny
The extracted item to sort by
Returns
Array[Any]
The sorted array.