How to sort an array of objects by multiple fields?

后端 未结 30 2373
北恋
北恋 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:06

    function sort(data, orderBy) {
            orderBy = Array.isArray(orderBy) ? orderBy : [orderBy];
            return data.sort((a, b) => {
                for (let i = 0, size = orderBy.length; i < size; i++) {
                    const key = Object.keys(orderBy[i])[0],
                        o = orderBy[i][key],
                        valueA = a[key],
                        valueB = b[key];
                    if (!(valueA || valueB)) {
                        console.error("the objects from the data passed does not have the key '" + key + "' passed on sort!");
                        return [];
                    }
                    if (+valueA === +valueA) {
                        return o.toLowerCase() === 'desc' ? valueB - valueA : valueA - valueB;
                    } else {
                        if (valueA.localeCompare(valueB) > 0) {
                            return o.toLowerCase() === 'desc' ? -1 : 1;
                        } else if (valueA.localeCompare(valueB) < 0) {
                            return o.toLowerCase() === 'desc' ? 1 : -1;
                        }
                    }
                }
            });
        }
    

    Using :

    sort(homes, [{city : 'asc'}, {price: 'desc'}])
    

    var homes = [
        {"h_id":"3",
         "city":"Dallas",
         "state":"TX",
         "zip":"75201",
         "price":"162500"},
        {"h_id":"4",
         "city":"Bevery Hills",
         "state":"CA",
         "zip":"90210",
         "price":"319250"},
        {"h_id":"6",
         "city":"Dallas",
         "state":"TX",
         "zip":"75000",
         "price":"556699"},
        {"h_id":"5",
         "city":"New York",
         "state":"NY",
         "zip":"00010",
         "price":"962500"}
        ];
    function sort(data, orderBy) {
                orderBy = Array.isArray(orderBy) ? orderBy : [orderBy];
                return data.sort((a, b) => {
                    for (let i = 0, size = orderBy.length; i < size; i++) {
                        const key = Object.keys(orderBy[i])[0],
                            o = orderBy[i][key],
                            valueA = a[key],
                            valueB = b[key];
                        if (!(valueA || valueB)) {
                            console.error("the objects from the data passed does not have the key '" + key + "' passed on sort!");
                            return [];
                        }
                        if (+valueA === +valueA) {
                            return o.toLowerCase() === 'desc' ? valueB - valueA : valueA - valueB;
                        } else {
                            if (valueA.localeCompare(valueB) > 0) {
                                return o.toLowerCase() === 'desc' ? -1 : 1;
                            } else if (valueA.localeCompare(valueB) < 0) {
                                return o.toLowerCase() === 'desc' ? 1 : -1;
                            }
                        }
                    }
                });
            }
    console.log(sort(homes, [{city : 'asc'}, {price: 'desc'}]));

提交回复
热议问题