How to sort an array of objects by multiple fields?

后端 未结 30 2336
北恋
北恋 2020-11-21 11:34

From this original question, how would I apply a sort on multiple fields?

Using this slightly adapted structure, how would I sort city (ascending) & then price (

30条回答
  •  天涯浪人
    2020-11-21 12:07

    The following function will allow you to sort an array of objects on one or multiple properties, either ascending (default) or descending on each property, and allow you to choose whether or not to perform case sensitive comparisons. By default, this function performs case insensitive sorts.

    The first argument must be the array containing the objects. The subsequent argument(s) must be a comma separated list of strings that reference the different object properties to sort by. The last argument (which is optional) is a boolean to choose whether or not to perform case sensitive sorts - use true for case sensitive sorts.

    The function will sort each property/key in ascending order by default. If you want a particular key to sort in descending order, then instead pass in an array in this format: ['property_name', true].

    Here are some sample uses of the function followed by an explanation (where homes is an array containing the objects):

    objSort(homes, 'city') --> sort by city (ascending, case in-sensitive)

    objSort(homes, ['city', true]) --> sort by city (descending, case in-sensitive)

    objSort(homes, 'city', true) --> sort by city then price (ascending, case sensitive)

    objSort(homes, 'city', 'price') --> sort by city then price (both ascending, case in-sensitive)

    objSort(homes, 'city', ['price', true]) --> sort by city (ascending) then price (descending), case in-sensitive)

    And without further ado, here's the function:

    function objSort() {
        var args = arguments,
            array = args[0],
            case_sensitive, keys_length, key, desc, a, b, i;
    
        if (typeof arguments[arguments.length - 1] === 'boolean') {
            case_sensitive = arguments[arguments.length - 1];
            keys_length = arguments.length - 1;
        } else {
            case_sensitive = false;
            keys_length = arguments.length;
        }
    
        return array.sort(function (obj1, obj2) {
            for (i = 1; i < keys_length; i++) {
                key = args[i];
                if (typeof key !== 'string') {
                    desc = key[1];
                    key = key[0];
                    a = obj1[args[i][0]];
                    b = obj2[args[i][0]];
                } else {
                    desc = false;
                    a = obj1[args[i]];
                    b = obj2[args[i]];
                }
    
                if (case_sensitive === false && typeof a === 'string') {
                    a = a.toLowerCase();
                    b = b.toLowerCase();
                }
    
                if (! desc) {
                    if (a < b) return -1;
                    if (a > b) return 1;
                } else {
                    if (a > b) return -1;
                    if (a < b) return 1;
                }
            }
            return 0;
        });
    } //end of objSort() function
    

    And here's some sample data:

    var homes = [{
        "h_id": "3",
        "city": "Dallas",
        "state": "TX",
        "zip": "75201",
        "price": 162500
    }, {
        "h_id": "4",
        "city": "Bevery Hills",
        "state": "CA",
        "zip": "90210",
        "price": 1000000
    }, {
        "h_id": "5",
        "city": "new york",
        "state": "NY",
        "zip": "00010",
        "price": 1000000
    }, {
        "h_id": "6",
        "city": "Dallas",
        "state": "TX",
        "zip": "85000",
        "price": 300000
    }, {
        "h_id": "7",
        "city": "New York",
        "state": "NY",
        "zip": "00020",
        "price": 345000
    }];
    

提交回复
热议问题