Version 3.17.2
Show:

ArraySort Class

Provides comparator functions useful for sorting arrays.

Item Index

Methods

Methods

_splitAlphaNum

(
  • string
)
String[] protected static

Defined in arraysort/js/arraysort.js:149

Available since 3.11.0

Splits a string into an array of alpha character and digit character parts.

Parameters:

  • string String

    String to split.

Returns:

String[]:

Array of alpha parts and digit parts.

Example:

Y.ArraySort._splitAlphaNum('abc123def456');
// => ['abc', '123', 'def', '456']

compare

(
  • a
  • b
  • desc
)
Boolean static

Comparator function for simple case-insensitive sorting of an array of strings.

Parameters:

  • a Object

    First sort argument.

  • b Object

    Second sort argument.

  • desc Boolean

    true if sort direction is descending, false if sort direction is ascending.

Returns:

Boolean:

-1 when a < b. 0 when a == b. 1 when a > b.

naturalCompare

(
  • a
  • b
  • [options]
)
Number static

Defined in arraysort/js/arraysort.js:65

Available since 3.11.0

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 | String

    First value to compare.

  • b Number | String

    Second value to compare.

  • [options] Object optional

    Options.

    • [caseSensitive=false] Boolean optional

      If true, a case-sensitive comparison will be performed. By default the comparison is case-insensitive.

    • [descending=false] Boolean optional

      If true, the sort order will be reversed so that larger values are sorted before smaller values.

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']