ArraySort Class
Provides comparator functions useful for sorting arrays.
Item Index
Methods
- _splitAlphaNum static
- compare static
- naturalCompare static
Methods
_splitAlphaNum
(
String[]
protected
static
-
string
Splits a string into an array of alpha character and digit character parts.
Parameters:
-
string
StringString to split.
Returns:
String[]:
Array of alpha parts and digit parts.
Example:
Y.ArraySort._splitAlphaNum('abc123def456');
// => ['abc', '123', 'def', '456']
compare
(
Boolean
static
-
a
-
b
-
desc
Comparator function for simple case-insensitive sorting of an array of strings.
Parameters:
Returns:
Boolean:
-1 when a < b. 0 when a == b. 1 when a > b.
naturalCompare
(
Number
static
-
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:
Returns:
Number:
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']