Sort by multiple keys and multiple orders in javascript

I need to sort an array of objects providing a dictionary with keys and their order. Similar:

var data = [
    { "name": "a", "age": 1, "money": 4 },
    { "name": "f", "age": 4, "money": 1 },
    { "name": "c", "age": 2, "money": 3 },
    { "name": "a", "age": 0, "money": 1},
    { "name": "f", "age": 4, "money": 3 },
    { "name": "c", "age": 1, "money": 4 },
    { "name": "c", "age": 3, "money": 1 }
];

var data = data.multiSort({
    name: "asc",
    age: "desc",
    money: "desc"
});

console.log(data);
/*
{ "name": "a", "age": 1, "money": 4 },
{ "name": "a", "age": 0, "money": 1},
{ "name": "c", "age": 3, "money": 1 }
{ "name": "c", "age": 2, "money": 3 },
{ "name": "c", "age": 1, "money": 4 },
{ "name": "f", "age": 4, "money": 3 },
{ "name": "f", "age": 4, "money": 1 }
*/

I am completely stuck, and I do not understand how to achieve this. Many people point to this simple piece of code, but I don’t understand how it should achieve what I am trying to do. https://github.com/Teun/thenBy.js

This is the code that I have right now. I know that I'm quite far from a solution, but I would appreciate any help to figure out how to get there, since I need to improve a lot on javascript.

Array.prototype.multiSort = function(sorters){

    function getNextSorter(sorters, currentSorterKey) {
        var sortersKeys = Object.keys(sorters);
        if(!currentSorterKey)
            currentSorterIndex = 0;
        else
            currentSorterIndex = sortersKeys.indexOf(currentSorterKey) + 1;

        var key = sortersKeys[currentSorterIndex];
        var order = sorters[key];
    }

    function compare(a, b, key, order) {
        var a = a[key];
        var b = b[key];

        //if both numbers compare them as numbers and not as strings
        var numericA = parseFloat(a);
        var numericB = parseFloat(b);
        if(!isNaN(numericA) && !isNaN(numericB)) {
            a = numericA;
            b = numericB;
        }

        //if different compare them with the given order
        if(a != b)
            return (order == "asc") ? a - b : b - a;
        //else compare next key as specified in sorters (if next key is present!)
        else
            //how to recursively call to get the next compare function?
    }

    //how to call sort now?

    return this;
};
+4
source share
2 answers

, . , - , .

. :. :

Array.prototype.multiSort = function() {
    // Make an array of arrays:
    // [  ["name" :"asc" ],
    //    ["age"  :"desc"],
    //    ["money":"desc"]   ]
    var sorters = Array.prototype.map.call(arguments, function(s) {
        return s.split(":");
    });

    function compare(a, b) {
        // Iterate over the sorters, and compare using the first non-equal values.
        for (var i = 0; i < sorters.length; i++) {
            var a_val = a[sorters[i][0]];
            var b_val = b[sorters[i][0]];
          
            if (a_val === b_val) {
                continue; // They're equal values, so try the next sorter
            }
            // Swap values if not ascending
            if (sorters[i][1] !== "asc") {
                var temp = a_val;
                a_val = b_val;
                b_val = temp;
            }
            // Use `.localeCompare()` if they're both strings
            if (typeof a_val === "string" && typeof b_val === "string") {
                return a_val.localeCompare(b_val);
            }
            return a_val - b_val;
        }
    }

    return this.sort(compare);
};

var data = [
    { "name": "a", "age": 1, "money": 4 },
    { "name": "f", "age": 4, "money": 1 },
    { "name": "c", "age": 2, "money": 3 },
    { "name": "a", "age": 0, "money": 1 },
    { "name": "f", "age": 4, "money": 3 },
    { "name": "c", "age": 1, "money": 4 },
    { "name": "c", "age": 3, "money": 1 }
];

var data = data.multiSort("name:asc", "age:desc", "money:desc");

document.body.innerHTML = "<pre>" + JSON.stringify(data, null, 4) + "</pre>";

, , :, .

compare sorters, a b, , . , .

. for - , .

+3

thenBy script - . , . . , , , , , .

, 3 .

// This sorts the name in ASC order
firstBy(function(d1, d2) { 
    if(d1.name < d2.name) return -1; 
    if(d1.name > d2.name) return 1;
    return 0;
})
// this sorts the age in DESC
// note that the > returns -1 but the < returns 1.
// flip these to sort in ASC
.thenBy(function(d1, d2) { 
    if(d1.age > d2.age) return -1;
    if(d1.age < d2.age) return 1;
    return 0;
 })
 // finally sort money in DESC order
 .thenBy(function(d1, d2) { 
    if(d1.money > d2.money) return -1;
    if(d1.money < d2.money) return 1;
    return 0;
 })

jsFiddle

+1

All Articles