ArraySort Class
yui3/src/arraysort/js/arraysort.js:13
Provides comparator functions useful for sorting arrays.
Index
Methods
- _splitAlphaNum static
- compare static
- naturalCompare static
Methods
_splitAlphaNum
-
string
Splits a string into an array of alpha character and digit character parts.
Parameters:
-
string
StringString to split.
Returns:
Array of alpha parts and digit parts.
Example:
Y.ArraySort._splitAlphaNum('abc123def456');
// => ['abc', '123', 'def', '456']
compare
-
a
-
b
-
desc
Comparator function for simple case-insensitive sorting of an array of strings.
Parameters:
-
a
ObjectFirst sort argument.
-
b
ObjectSecond sort argument.
-
desc
Booleantrue
if sort direction is descending,false
if sort direction is ascending.
Returns:
-1 when a < b. 0 when a == b. 1 when a > b.
naturalCompare
-
a
-
b
-
[options]
Performs a natural-order comparison of two strings or numbers (or a string and a number). This ensures that a value like 'foo2' will be sorted before 'foo10', whereas a standard ASCII sort would sort 'foo10' first.
Parameters:
-
a
Number | StringFirst value to compare.
-
b
Number | StringSecond value to compare.
-
[options]
Object optionalOptions.
-
[caseSensitive=false]
Boolean optionalIf
true
, a case-sensitive comparison will be performed. By default the comparison is case-insensitive. -
[descending=false]
Boolean optionalIf
true
, the sort order will be reversed so that larger values are sorted before smaller values.
-
Returns:
0
if the two items are equal, a negative number if a
should be sorted before b, or a positive number if b should be
sorted before a.
Example:
var items = ['item10', 'item2', 'item1', 10, '1', 2];
items.sort(Y.ArraySort.naturalCompare);
console.log(items); // => ['1', 2, 10, 'item1', 'item2', 'item10']