Merge branch 'MDL-32509' of git://github.com/danpoltawski/moodle
[moodle.git] / lib / yui / 3.5.0 / build / arraysort / arraysort.js
blob2d5e5baf66296d124b371ead1c860403f902cce2
1 /*
2 YUI 3.5.0 (build 5089)
3 Copyright 2012 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
7 YUI.add('arraysort', function(Y) {
9 /**
10 Provides a case-insenstive comparator which can be used for array sorting.
12 @module arraysort
15 var LANG = Y.Lang,
16     ISVALUE = LANG.isValue,
17     ISSTRING = LANG.isString;
19 /**
20 Provides a case-insenstive comparator which can be used for array sorting.
22 @class ArraySort
25 Y.ArraySort = {
27     /**
28     Comparator function for simple case-insensitive sorting of an array of
29     strings.
31     @method compare
32     @param a {Object} First sort argument.
33     @param b {Object} Second sort argument.
34     @param desc {Boolean} `true` if sort direction is descending, `false` if
35         sort direction is ascending.
36     @return {Boolean} -1 when a < b. 0 when a == b. 1 when a > b.
37     */
38     compare: function(a, b, desc) {
39         if(!ISVALUE(a)) {
40             if(!ISVALUE(b)) {
41                 return 0;
42             }
43             else {
44                 return 1;
45             }
46         }
47         else if(!ISVALUE(b)) {
48             return -1;
49         }
51         if(ISSTRING(a)) {
52             a = a.toLowerCase();
53         }
54         if(ISSTRING(b)) {
55             b = b.toLowerCase();
56         }
57         if(a < b) {
58             return (desc) ? 1 : -1;
59         }
60         else if (a > b) {
61             return (desc) ? -1 : 1;
62         }
63         else {
64             return 0;
65         }
66     }
71 }, '3.5.0' ,{requires:['yui-base']});