简体   繁体   English

按对象键按Javascript /下划线的降序排序

[英]Sort by object key in descending order on Javascript/underscore

I have the following object array where the key is the date in UTC format. 我有以下对象数组,其中键是UTC格式的日期。

    Array = [{1436796000000:["Task1","Task2"],
         1437400800000:["Task4","Task8"],
         1436968800000: ["Task3","Task2"],
         1436882400000:["Task5","Task6"]}]

I want to sort this array object by key in descending order. 我想按键降序排序这个数组对象。 So the expected output will be following like the latest date will come first. 因此预期的输出将跟随最新的日期将首先出现。

    Array = [{1437400800000:["Task4","Task8"],
             1436968800000: ["Task3","Task2"],
             1436882400000:["Task5","Task6"],
             1436796000000:["Task1","Task2"]}]

How can I do this in javascript or using underscore.js? 我怎么能在javascript或使用underscore.js这样做?

No, that isn't an array, it's an object, and Javascript objects' properties are unordered by definition; 不,这不是一个数组,它是一个对象,Javascript对象的属性按定义是无序的; so sorting them is meaningless. 所以对它们进行排序毫无意义。

You could instead use an array, which does have order, and restructure your data like this: 您可以改为使用具有顺序的数组,并重构您的数据,如下所示:

var arr = [
  { date: 1436796000000, value: ["Task1","Task2"] },
  { date: 1437400800000, value: ["Task4","Task8"] },
  { date: 1436968800000, value: ["Task3","Task2"] },
  { date: 1436882400000, value: ["Task5","Task6"] }
]

and then you can sort it by date: 然后你可以按日期排序:

arr.sort( function ( a, b ) { return b.date - a.date; } );

If you don't want to restructure your data, you can iterate through it in the order you want, by getting an array of its keys and sorting that array, then using that array to access your object's properties, but you will need to do this each time your want to iterate through it in a particular order, since there is still no order information stored in the object: 如果您不想重构数据,可以按照您想要的顺序迭代它,通过获取其键的数组并对该数组进行排序,然后使用该数组访问对象的属性,但您需要执行此操作这是每次你想以特定的顺序迭代它时,因为对象中仍然没有存储订单信息:

// Get the array of keys
var keys = Object.keys( obj );

// Sort the keys in descending order
keys.sort( function ( a, b ) { return b - a; } );

// Iterate through the array of keys and access the corresponding object properties
for ( var i = 0; i < keys.length; i++ ) {
    console.log( keys[i], obj[ keys[i] ] );
}

You will need to shim Object.keys to support IE 8 and older browsers. 您需要使用Shim Object.keys来支持IE 8和更旧的浏览器。

In Paulpro answer, I edit javascript array sort function (easy to understand): 在Paulpro的回答中,我编辑了javascript数组排序函数(简单易懂):

function compare(a,b) {
   if (a.date < b.date )
     return -1;
   if (a.date  > b.date )
    return 1;
   return 0;
}
arr.sort(compare);

Here is my example: enter link description here 这是我的例子: 在这里输入链接描述

Here is relative post: enter link description here 这是相对的帖子: 在这里输入链接描述

Here is what I did for my use case hope this helps 以下是我为我的用例所做的事情希望这会有所帮助

 var list = {30: "103", 40: "75", 50: "116", 100: "15"}; // Reverse sorting on key const keysSorted = Object.keys(list).sort(function(a,b){return ba}) console.log(keysSorted); const arr = []; // Adding the sorted result to an array of object for (let i=0; i<keysSorted.length;i++) { const obj = {}; obj.per= keysSorted[i]; obj.val= list[keysSorted[i]]; arr.push(obj); } console.log(arr); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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