2
YUI 3.13.0 (build 508226d)
3
Copyright 2013 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
8
YUI.add('arraysort', function (Y, NAME) {
10
/*jshint expr:true, onevar:false */
13
Provides comparator functions useful for sorting arrays.
19
ISVALUE = LANG.isValue,
20
ISSTRING = LANG.isString;
23
Provides comparator functions useful for sorting arrays.
29
var ArraySort = Y.ArraySort = {
30
// -- Public Methods -------------------------------------------------------
33
Comparator function for simple case-insensitive sorting of an array of
37
@param a {Object} First sort argument.
38
@param b {Object} Second sort argument.
39
@param desc {Boolean} `true` if sort direction is descending, `false` if
40
sort direction is ascending.
41
@return {Boolean} -1 when a < b. 0 when a == b. 1 when a > b.
44
compare: function(a, b, desc) {
53
else if(!ISVALUE(b)) {
64
return (desc) ? 1 : -1;
67
return (desc) ? -1 : 1;
75
Performs a natural-order comparison of two strings or numbers (or a string
76
and a number). This ensures that a value like 'foo2' will be sorted before
77
'foo10', whereas a standard ASCII sort would sort 'foo10' first.
81
var items = ['item10', 'item2', 'item1', 10, '1', 2];
83
items.sort(Y.ArraySort.naturalCompare);
84
console.log(items); // => ['1', 2, 10, 'item1', 'item2', 'item10']
86
@method naturalCompare
87
@param {Number|String} a First value to compare.
88
@param {Number|String} b Second value to compare.
89
@param {Object} [options] Options.
90
@param {Boolean} [options.caseSensitive=false] If `true`, a
91
case-sensitive comparison will be performed. By default the
92
comparison is case-insensitive.
93
@param {Boolean} [options.descending=false] If `true`, the sort order
94
will be reversed so that larger values are sorted before smaller
96
@return {Number} `0` if the two items are equal, a negative number if _a_
97
should be sorted before _b_, or a positive number if _b_ should be
102
naturalCompare: function (a, b, options) {
103
// Coerce `a` and `b` to strings.
107
// Convert `a` and `b` to lowercase unless `options.caseSensitive` is
109
if (!options || !options.caseSensitive) {
114
// Split `a` and `b` into alpha parts and numeric parts.
115
var aParts = ArraySort._splitAlphaNum(a),
116
bParts = ArraySort._splitAlphaNum(b),
117
length = Math.min(aParts.length, bParts.length),
124
// Compare each part of `a` with each part of `b`.
125
for (i = 0; i < length; i++) {
129
// If the two parts aren't equal, compare them and stop iterating.
130
if (aPart !== bPart) {
131
// First, try comparing them as numbers.
132
result = aPart - bPart;
134
// If that didn't work, compare them as strings. This falsiness
135
// check works because `result` can't be 0 (we checked for
136
// equality above) and NaN is falsy.
138
result = aPart > bPart ? 1 : -1;
141
// At this point we know enough to be able to sort the two
142
// strings, so we don't need to compare any more parts.
147
// If we get here and `result` is still 0, then sort the shorter string
148
// before the longer string.
149
result || (result = a.length - b.length);
151
// Return the result, flipping the order if `options.descending` is
153
return options && options.descending ? -result : result;
156
// -- Protected Methods ----------------------------------------------------
159
Splits a string into an array of alpha character and digit character parts.
163
Y.ArraySort._splitAlphaNum('abc123def456');
164
// => ['abc', '123', 'def', '456']
166
@method _splitAlphaNum
167
@param {String} string String to split.
168
@return {String[]} Array of alpha parts and digit parts.
173
_splitAlphaNum: function (string) {
174
/*jshint boss:true */
176
regex = /(\d+|\D+)/g,
179
while (match = regex.exec(string)) { // assignment
180
parts.push(match[1]);
188
}, '3.13.0', {"requires": ["yui-base"]});