Joined January 2014
·

Tom Strader

Orange County, California Area
·

Updated to handle numbers as strings:

if( typeof helper == 'undefined' ) {
  var helper = { } ;
}

helper.arr = {
         /**
     * Function to sort multidimensional array
     * 
     * <a href="/param">@param</a> {array} [arr] Source array
     * <a href="/param">@param</a> {array} [columns] List of columns to sort
     * <a href="/param">@param</a> {array} [order_by] List of directions (ASC, DESC)
     * @returns {array}
     */
    multisort: function(arr, columns, order_by) {
        if(typeof columns == 'undefined') {
            columns = []
            for(x=0;x<arr[0].length;x++) {
                columns.push(x);
            }
        }

        if(typeof order_by == 'undefined') {
            order_by = []
            for(x=0;x<arr[0].length;x++) {
                order_by.push('ASC');
            }
        }

        function multisort_recursive(a,b,columns,order_by,index) {  
            var direction = order_by[index] == 'DESC' ? 1 : 0;

            var is_numeric = !isNaN(+a[columns[index]] - +b[columns[index]]);


            var x = is_numeric ? +a[columns[index]] : a[columns[index]].toLowerCase();
            var y = is_numeric ? +b[columns[index]] : b[columns[index]].toLowerCase();



            if(x < y) {
                    return direction == 0 ? -1 : 1;
            }

            if(x == y)  {               
                return columns.length-1 > index ? multisort_recursive(a,b,columns,order_by,index+1) : 0;
            }

            return direction == 0 ? 1 : -1;
        }

        return arr.sort(function (a,b) {
            return multisort_recursive(a,b,columns,order_by,0);
        });
    }
};
Achievements
1 Karma
0 Total ProTip Views