Javascript 如何按值对(关联)数组进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12788051/
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
How to sort an (associative) array by value?
提问by PopeJohnPaulII
Possible Duplicate:
How to sort an associative array by its values in Javascript?
So first off I know technically Javascript doesn't have associative arrays, but I wasn't sure how to title it and get the right idea across.
所以首先我知道技术上 Javascript 没有关联数组,但我不确定如何命名它并传达正确的想法。
So here is the code I have,
所以这是我的代码,
var status = new Array();
status['BOB'] = 10
status['TOM'] = 3
status['ROB'] = 22
status['JON'] = 7
And I want to sort it by value such that when I loop through it later on ROB
comes first, then BOB
, etc.
我想按值对它进行排序,这样当我稍后循环遍历它时,ROB
首先出现,然后BOB
等等。
I've tried,
我试过了,
status.sort()
status.sort(function(a, b){return a[1] - b[1];});
But none of them seem to actually DO anything.
但他们似乎都没有真正做任何事情。
Printing the array out to the console before and after result in it coming out in the same order.
将数组打印到控制台之前和之后会导致它以相同的顺序出现。
回答by saml
Arrays can only have numeric indexes. You'd need to rewrite this as either an Object, or an Array of Objects.
数组只能有数字索引。您需要将其重写为对象或对象数组。
var status = new Object();
status['BOB'] = 10
status['TOM'] = 3
status['ROB'] = 22
status['JON'] = 7
or
或者
var status = new Array();
status.push({name: 'BOB', val: 10});
status.push({name: 'TOM', val: 3});
status.push({name: 'ROB', val: 22});
status.push({name: 'JON', val: 7});
If you like the status.push
method, you can sort it with:
如果您喜欢该status.push
方法,可以使用以下方法对其进行排序:
status.sort(function(a,b) {
return a.val - b.val;
});
回答by Alex Wayne
"Arrays" in javascript are numerically indexed. "Objects" are what you would would call associative array. "Objects" are inherently unordered.
javascript 中的“数组”是数字索引的。“对象”就是你所说的关联数组。“对象”本质上是无序的。
So to do this you would need to change your data structure a bit.
所以要做到这一点,你需要稍微改变你的数据结构。
var arr = []; // Array
// push a few objects to the array.
arr.push({name: 'BOB', age: 10});
arr.push({name: 'TOM', age: 3});
arr.push({name: 'ROB', age: 22});
arr.push({name: 'JON', age: 7});
var sorted = arr.sort(function(a, b) {
return a.age - b.age;
});
console.log(sorted);
回答by A. Matías Quezada
First - It's incorrect to create a Array to use it as a Mapping. Instead use Object:
首先 - 创建一个数组以将其用作映射是不正确的。而是使用对象:
var status = {};
status['BOB'] = 10;
The Array is a class with logic to handle numeric-sequential indexes.
Array 是一个具有处理数字顺序索引逻辑的类。
Now answering your response, Mappings have no order, there is no guarantee about the order it will iterate so there is no such thing as "sorting a mapping"
现在回答你的回答,映射没有顺序,不能保证它会迭代的顺序,所以没有“排序映射”这样的东西
I sugest you to use a array with objects with key, value:
我建议您使用带有键值对象的数组:
var status = [];
status.push({ key: 'BOB', value: 10 });
Or two arrays:
或两个数组:
var status = {
keys: [],
values: []
};
status.keys.push('BOB');
status.value.push(10);
It's up to your needs.
这取决于您的需求。
回答by Anoop
You should use JavaScript Object inside Array: jsfidle
你应该在 Array 中使用 JavaScript 对象:jsfidle
var status = new Array();
status.push({key: 'BOB', value: 10});
status.push({key: 'TOM', value: 3});
status.push({key: 'ROB', value: 22});
status.push({key: 'JON', value: 7});
status.sort(function(a, b){
if(a.value > b.value){
return -1;
}
else if(a.value < b.value){
return 1;
}
return 0;
});