简体   繁体   中英

Javascript sort by nested object of array

    {
        "a": [
            {
            "value1": 34,
            "value2": 44
            },
            {
            "value1": 33,
            "value2": 10
            },
            {
            "value1": 52,
            "value2": 53
            }
        ],
        "b": [
            {
            "value1": 12,
            "value2": 43
            },
            {
            "value1": 23,
            "value2": 78
            },
            {
            "value1": 98,
            "value2": 36
            }
        ],
        "c": [
            {
            "value1": 56,
            "value2": 90
            },
            {
            "value1": 76,
            "value2": 50
            },
            {
            "value1": 20,
            "value2": 30
            }
        ]
    }

Here is my data. I am trying to sort this data by "value2" of last object of array.

So. Here "value2" of "c" is 30 which is small so that object should come first.

The expected result is:

["c", "b", "a"]

So "value2" order is is 30 then 36 then 53

I can't think of any way to do this so asking here without attempt.

Please take a look how we can achieve this.

You can do it in this way

 const data = { a: [ { value1: 34, value2: 44, }, { value1: 33, value2: 10, }, { value1: 52, value2: 53, }, ], b: [ { value1: 12, value2: 43, }, { value1: 23, value2: 78, }, { value1: 98, value2: 36, }, ], c: [ { value1: 56, value2: 90, }, { value1: 76, value2: 50, }, { value1: 20, value2: 30, }, ], }; const sortData = (data) => { const obj = {}; for (const i in data) { obj[i] = data[i][data[i].length - 1]["value2"]; } return Object.entries(obj).sort(([, a], [, b]) => a - b).reduce((r, [k]) => ({...r, [k]: data[k] }), {}); }; const res = sortData(data); console.log(res);

By nature, Objects don't inherently have a guaranteed sort order, however, we can try to work around that which should work in most cases.

Objects don't have a .sort() method, but we can use Object.entries() to temporarily convert the object to an array, apply our desired sorting logic, and then reconvert the final product to an object using Object.fromEntries() .

This should do what you're looking for:

const sortObjectByValue2 = obj => Object.fromEntries(Object.entries(obj).sort((a,b) => a[1].slice().reverse()[0].value2 - b[1].slice().reverse()[0].value2));

Here it is in action:

 const sortObjectByValue2 = obj => Object.fromEntries(Object.entries(obj).sort((a,b) => a[1].slice().reverse()[0].value2 - b[1].slice().reverse()[0].value2)); const values = { a: [{ value1: 34, value2: 44 }, { value1: 33, value2: 10 }, { value1: 52, value2: 53 }], b: [{ value1: 12, value2: 43 }, { value1: 23, value2: 78 }, { value1: 98, value2: 36 }], c: [{ value1: 56, value2: 90 }, { value1: 76, value2: 50 }, { value1: 20, value2: 30 }] } console.log(sortObjectByValue2(values));

More information on Object.entries() and Object.fromEntries() can be found at these links:

I think you can store the keys of the object to an array, and then sorted the array by the object vaule. Could use the code like the following:

const unordered  = {
    "a": [
        {
        "value1": 34,
        "value2": 44
        },
        {
        "value1": 33,
        "value2": 10
        },
        {
        "value1": 52,
        "value2": 53
        }
    ],
    "b": [
        {
        "value1": 12,
        "value2": 43
        },
        {
        "value1": 23,
        "value2": 78
        },
        {
        "value1": 98,
        "value2": 36
        }
    ],
    "c": [
        {
        "value1": 56,
        "value2": 90
        },
        {
        "value1": 76,
        "value2": 50
        },
        {
        "value1": 20,
        "value2": 30
        }
    ]
};

let ordered = Object.keys(unordered).sort((k1,k2)=>{
        let arr1 = unordered[k1];
        let arr2 = unordered[k2];
        if(arr1[arr1.length-1]["value2"]>arr2[arr2.length-1]["value2"]){
            return 1;
        }else{
            return -1;
        }
    });

console.log(ordered);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM