lib/sortedArray.js
function sortedArray(arr, comparator1, comparator2, comparator3) {
    var _ = {};

    arr = arr || [];

    if (typeof arr._sortedArray === "object") {
        throw new Error("(sortedArray) Cannot extend array: Special key _sortedArray is already defined. Did you apply it twice?");
    }
    arr._sortedArray = _;

    _.reversed = false;

    _.push = arr.push;
    arr.push = push;
    _.unshift = arr.unshift;
    _.splice = arr.splice;
    arr.splice = splice;
    arr.unshift = push;
    _.indexOf = arr.indexOf;
    arr.indexOf = indexOf;
    _.sort = arr.sort;
    arr.sort = sort;
    _.reverse = arr.reverse;
    arr.reverse = reverse;
    arr.sortedIndex = sortedIndex;

    if (arguments.length === 1) {
        arr.comparator = arr.comparator || defaultComparator;
    } else if (arguments.length === 2) {
        arr.comparator = comparator1;
    } else {
        arr.comparator = multipleComparators(slice.call(arguments, 1));
    }

    if (arr.length > 0) {
        arr.sort();
    }

    return arr;
}
function sortedArray(arr, comparator1, comparator2, comparator3) {
    var _ = {};

    arr = arr || [];

    if (typeof arr._sortedArray === "object") {
        throw new Error("(sortedArray) Cannot extend array: Special key _sortedArray is already defined. Did you apply it twice?");
    }
    arr._sortedArray = _;

    _.reversed = false;

    _.push = arr.push;
    arr.push = push;
    _.unshift = arr.unshift;
    _.splice = arr.splice;
    arr.splice = splice;
    arr.unshift = push;
    _.indexOf = arr.indexOf;
    arr.indexOf = indexOf;
    _.sort = arr.sort;
    arr.sort = sort;
    _.reverse = arr.reverse;
    arr.reverse = reverse;
    arr.sortedIndex = sortedIndex;

    if (arguments.length === 1) {
        arr.comparator = arr.comparator || defaultComparator;
    } else if (arguments.length === 2) {
        arr.comparator = comparator1;
    } else {
        arr.comparator = multipleComparators(slice.call(arguments, 1));
    }

    if (arr.length > 0) {
        arr.sort();
    }

    return arr;
}
function sortedArray ( arr , comparator1 , comparator2 , comparator3 ) { var _ = { } ; arr = arr || [ ] ; if ( typeof arr . _sortedArray === "object" ) { throw new Error ( "(sortedArray) Cannot extend array: Special key _sortedArray is already defined. Did you apply it twice?" ) ; } arr . _sortedArray = _ ; _ . reversed = false ; _ . push = arr . push ; arr . push = push ; _ . unshift = arr . unshift ; _ . splice = arr . splice ; arr . splice = splice ; arr . unshift = push ; _ . indexOf = arr . indexOf ; arr . indexOf = indexOf ; _ . sort = arr . sort ; arr . sort = sort ; _ . reverse = arr . reverse ; arr . reverse = reverse ; arr . sortedIndex = sortedIndex ; if ( arguments . length === 1 ) { arr . comparator = arr . comparator || defaultComparator ; } else if ( arguments . length === 2 ) { arr . comparator = comparator1 ; } else { arr . comparator = multipleComparators ( slice . call ( arguments , 1 ) ) ; } if ( arr . length > 0 ) { arr . sort ( ) ; } return arr ; }
Turns an array or every object with an array-like interface into a sorted array that maintains the sort order.
This is basically achieved by replacing the original mutator methods with versions that respect the order.

If the supplied array has a comparator-function, this function will be used for comparison.

You may also provide multiple comparators. If comparator1 returns 0, comparator2 is applied, etc.

@param {Array|Object} arr
@param {Function=} comparator1
@param {Function=} comparator2
@param {Function=} comparator3
@returns {Array|Object}
Turns an array or every object with an array - like interface into a sorted array that maintains the sort order . This is basically achieved by replacing the original mutator methods with versions that respect the order .