Javascript 在Javascript/下划线按对象键降序排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31553155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort by object key in descending order on Javascript/underscore
提问by Farhad Yasir
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 执行此操作?
回答by Paul
No, that isn't an array, it's an object, and Javascript objects' properties are unordered by definition; so sorting them is meaningless.
不,那不是数组,而是对象,Javascript 对象的属性根据定义是无序的;所以对它们进行排序是没有意义的。
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.keysto support IE 8 and older browsers.
您需要填充Object.keys以支持 IE 8 和更旧的浏览器。
回答by Giau Huynh
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
这是相关帖子:在此处输入链接描述
回答by nipun
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 b-a})
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);