Sort array of objects by single key with date value

后端 未结 19 1380
情话喂你
情话喂你 2020-11-22 10:56

I have an array of objects with several key value pairs, and I need to sort them based on \'updated_at\':

[
    {
        \"updated_at\" : \"2012-01-01T06:25         


        
19条回答
  •  逝去的感伤
    2020-11-22 11:53

    I already answered a really similar question here: Simple function to sort an array of objects

    For that question I created this little function that might do what you want:

    function sortByKey(array, key) {
        return array.sort(function(a, b) {
            var x = a[key]; var y = b[key];
            return ((x < y) ? -1 : ((x > y) ? 1 : 0));
        });
    }
    

提交回复
热议问题