Javascript 在javascript中,是否有一种简单的方法可以按值对键值对进行排序并返回键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4969121/
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
in javascript, is there an easy way to sort key-value pairs by the value, and return the key?
提问by David Rhoden
In javascript, is there an easy way to sort key-value pairs by the value (assume the value is numeric), and return the key? A jQuery way to do this would be useful as well.
在javascript中,是否有一种简单的方法可以按值(假设值是数字)对键值对进行排序,然后返回键?一种 jQuery 方法来做到这一点也很有用。
(There are a lot of related questions about key-value pairs here, but I can't find one specifically about sorting.)
(这里有很多关于键值对的相关问题,但我找不到一个专门关于排序的问题。)
回答by Tim Down
There's nothing easy to do this cross-browser. Assuming an array such as
做这个跨浏览器并不容易。假设一个数组,例如
var a = [
{key: "foo", value: 10},
{key: "bar", value: 1},
{key: "baz", value: 5}
];
... you can get an array of the key
properties sorted by value
as follows:
...您可以获得key
按value
如下方式排序的属性数组:
var sorted = a.slice(0).sort(function(a, b) {
return a.value - b.value;
});
var keys = [];
for (var i = 0, len = sorted.length; i < len; ++i) {
keys[i] = sorted[i].key;
}
// keys is ["bar", "baz", "foo"];
回答by jAndy
Let's assume we have an Array
of Objects
, like:
假设我们有一个Array
of Objects
,例如:
var data = [
{foo: 6},
{foo: 2},
{foo: 13},
{foo: 8}
];
We can call Array.prototype.sort()
help, use Array.prototype.map()
helpto map a new array and Object.keys()
helpto grab the key:
我们可以调用Array.prototype.sort()
help,使用Array.prototype.map()
help映射一个新数组并Object.keys()
帮助获取密钥:
var keys = data.sort(function(a,b) {
return a.foo - b.foo;
}).map(function(elem, index, arr) {
return Object.keys(elem)[0];
});
Be aware of, Array.prototype.map()
requires Javascript 1.6and Object.keys()
is ECMAscript5 (requires Javascript 1.8.5).
请注意,Array.prototype.map()
需要 Javascript 1.6并且Object.keys()
是 ECMAscript5(需要 Javascript 1.8.5)。
You'll find alternative code for all those methods on MDC.
您将在 MDC 上找到所有这些方法的替代代码。
回答by Spudley
As far as I know, there isn't a built-in Javascript function to sort an array by its keys.
据我所知,没有内置的 Javascript 函数可以按键对数组进行排序。
However, it shouldn't take too much code to do it: just extract the keys into their own array, sort them using the normal sort
function, and rebuild the array in the right order. Something like this should do the trick:
但是,它不应该花费太多代码来完成:只需将键提取到它们自己的数组中,使用普通sort
函数对它们进行排序,然后以正确的顺序重建数组。像这样的事情应该可以解决问题:
function SortArrayByKeys(inputarray) {
var arraykeys=[];
for(var k in inputarray) {arraykeys.push(k);}
arraykeys.sort();
var outputarray=[];
for(var i=0; i<arraykeys.length; i++) {
outputarray[arraykeys[i]]=inputarray[arraykeys[i]];
}
return outputarray;
}
Now you can just call your function like so:
现在你可以像这样调用你的函数:
var myarray = {'eee':12, 'blah':34 'what'=>66, 'spoon':11, 'snarglies':22};
myarray = SortArrayByKeys(myarray);
And the output will be:
输出将是:
{'blah':34, 'eee':12, 'spoon':11, 'snarglies':22, 'what':66}
Hope that helps.
希望有帮助。
Working test page here: http://jsfiddle.net/6Ev3S/
这里的工作测试页面:http: //jsfiddle.net/6Ev3S/
回答by shyguy
Given
给定的
var object = {
'a': 5,
'b': 11,
'c': 1,
'd': 2,
'e': 6
}
You can sort object
's keys by their values using the following:
您可以object
使用以下方法按值对的键进行排序:
Object.keys(object).sort(function (a, b) {
return object[a] - object[b]
}))
Result
结果
[ 'c', 'd', 'a', 'e', 'b' ]
回答by kennebec
If you can't count on the exended array and object properties,
如果你不能指望扩展数组和对象属性,
you can use the original Array methods-
您可以使用原始的 Array 方法-
function keysbyValue(O){
var A= [];
for(var p in O){
if(O.hasOwnProperty(p)) A.push([p, O[p]]);
}
A.sort(function(a, b){
var a1= a[1], b1= b[1];
return a1-b1;
});
for(var i= 0, L= A.length; i<L; i++){
A[i]= A[i][0];
}
return A;
}
//test
var Obj={a: 20, b: 2, c: 100, d: 10, e: -10};
keysbyValue(Obj)
/* returned value: (Array)
e,b,d,a,c
*/