如果我有一个JavaScript对象,如:
var list = {
"you": 100,
"me": 75,
"foo": 116,
"bar": 15
};
是否有一种方法可以根据值对属性进行排序?最后得到
list = {
"bar": 15,
"me": 75,
"you": 100,
"foo": 116
};
如果我有一个JavaScript对象,如:
var list = {
"you": 100,
"me": 75,
"foo": 116,
"bar": 15
};
是否有一种方法可以根据值对属性进行排序?最后得到
list = {
"bar": 15,
"me": 75,
"you": 100,
"foo": 116
};
当前回答
你的对象可以有任意数量的属性,如果你把对象放在数组中,你可以选择根据你想要的任何对象属性进行排序,数字或字符串。考虑这个数组:
var arrayOfObjects = [
{
name: 'Diana',
born: 1373925600000, // Mon, Jul 15 2013
num: 4,
sex: 'female'
},
{
name: 'Beyonce',
born: 1366832953000, // Wed, Apr 24 2013
num: 2,
sex: 'female'
},
{
name: 'Albert',
born: 1370288700000, // Mon, Jun 3 2013
num: 3,
sex: 'male'
},
{
name: 'Doris',
born: 1354412087000, // Sat, Dec 1 2012
num: 1,
sex: 'female'
}
];
按出生日期排序,最年长的先
// use slice() to copy the array and not just make a reference
var byDate = arrayOfObjects.slice(0);
byDate.sort(function(a,b) {
return a.born - b.born;
});
console.log('by date:');
console.log(byDate);
按名称排序
var byName = arrayOfObjects.slice(0);
byName.sort(function(a,b) {
var x = a.name.toLowerCase();
var y = b.name.toLowerCase();
return x < y ? -1 : x > y ? 1 : 0;
});
console.log('by name:');
console.log(byName);
http://jsfiddle.net/xsM5s/16/
其他回答
对象按值排序(DESC)
function sortObject(list) {
var sortable = [];
for (var key in list) {
sortable.push([key, list[key]]);
}
sortable.sort(function(a, b) {
return (a[1] > b[1] ? -1 : (a[1] < b[1] ? 1 : 0));
});
var orderedList = {};
for (var i = 0; i < sortable.length; i++) {
orderedList[sortable[i][0]] = sortable[i][1];
}
return orderedList;
}
我遵循slebetman给出的解决方案(去阅读它的所有细节),但调整,因为你的对象是非嵌套的。
// First create the array of keys/values so that we can sort it:
var sort_array = [];
for (var key in list) {
sort_array.push({key:key,value:list[key]});
}
// Now sort it:
sort_array.sort(function(x,y){return x.value - y.value});
// Now process that object with it:
for (var i=0;i<sort_array.length;i++) {
var item = list[sort_array[i].key];
// now do stuff with each item
}
找出每个元素的频率,并按频率/值进行排序。
Let response =["苹果","橘子","苹果","香蕉","橘子","香蕉","香蕉"]; 设frequency = {}; response.forEach(函数(项){ 频率[项目]=频率[项目]?频率[项]+ 1:1; }); console.log(频率); let intents = Object.entries(frequency) .sort((a, b) => b[1] - a[1]) . map(函数(x) { 返回x [0]; }); console.log(意图);
输出:
{ apple: 2, orange: 2, banana: 3 }
[ 'banana', 'apple', 'orange' ]
非常简短简单!
var sortedList = {};
Object.keys(list).sort((a,b) => list[a]-list[b]).forEach((key) => {
sortedList[key] = list[key]; });
谢谢你,继续回答@Nosredna
现在我们知道对象需要转换为数组,然后对数组排序。这对于按字符串排序数组(或转换对象为数组)非常有用:
Object {6: Object, 7: Object, 8: Object, 9: Object, 10: Object, 11: Object, 12: Object}
6: Object
id: "6"
name: "PhD"
obe_service_type_id: "2"
__proto__: Object
7: Object
id: "7"
name: "BVC (BPTC)"
obe_service_type_id: "2"
__proto__: Object
//Sort options
var sortable = [];
for (var vehicle in options)
sortable.push([vehicle, options[vehicle]]);
sortable.sort(function(a, b) {
return a[1].name < b[1].name ? -1 : 1;
});
//sortable => prints
[Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2]]
0: Array[2]
0: "11"
1: Object
id: "11"
name: "AS/A2"
obe_service_type_id: "2"
__proto__: Object
length: 2
__proto__: Array[0]
1: Array[2]
0: "7"
1: Object
id: "7"
name: "BVC (BPTC)"
obe_service_type_id: "2"
__proto__: Object
length: 2