如何使用对象属性对对象数组进行排序 [英] How to sort an array of objects using their object properties
本文介绍了如何使用对象属性对对象数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个带有属性的对象数组,并且想按一个特定的属性对这些对象进行排序,那我该怎么做呢?例如,假设我有一堆news
对象,每个对象都有一个date
属性.
If I have an array of objects with properties and I wanted to sort the objects by a specific property, how would I be able to do this? For example, let's say that I had a bunch of news
objects each with a date
property.
我如何能够按日期对javascript中的每个对象进行排序?
How would I be able to sort each of the objects in javascript by date?
推荐答案
如果您的数组是arr:
If your array is arr:
arr.sort(function(a,b) {
return ( a.date < b.date ? -1 : (a.date > b.date ? 1 : 0 ) );
});
这篇关于如何使用对象属性对对象数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文