javascript 如何对我的 JSON 数组进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8924456/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:01:13  来源:igfitidea点击:

How can I sort my JSON array?

javascriptjqueryjson

提问by Vikas Naranje

How to output this JSON element in correct order by it's value?

如何按其值按正确顺序输出此 JSON 元素?

var json = { 
    "message": {
        "90": "Adidas", 
        "2": "Armani", 
        "89": "Casio", 
        "1": "Diesel", 
        "72": "DKNY", 
        "88": "Fossil", 
        "4": "Hamilton", 
        "6": "Luminox", 
        "99": "Michael Kors", 
        "968": "Mont Blanc Pens", 
        "3": "Nixon", 
        "959": "Nooka", 
        "92": "Seiko", 
        "91": "Tendence", 
        "7": "Tissot" 
    } 
};

var str = '';
for (var i in json.message) {
    str += json.message[i]+'\n';
}
alert(str);

it alerts in the below order -

它按以下顺序发出警报 -

Diesel
Armani
Nixon
Hamilton
Luminox
DKNY
Fossil
Casio
Adidas
Tendence
Seiko
Michael Kors
Nooka
Mont Blanc Pens

Diesel
Armani
Nixon
Hamilton
Luminox
DKNY
Fossil
Casio
Adidas
Tendence
Seiko
Michael Kors
Nooka
Mont Blanc Pens

But I want it in below order

但我想按以下顺序

Adidas
Armani
Casio
Diesel
DKNY
Fossil
Hamilton
Luminox
Michael Kors
Mont Blanc Pens
Nixon
Nooka
Seiko
Tendence
Tissot

Adidas
Armani
Casio
Diesel
DKNY
Fossil
Hamilton
Luminox
Michael Kors
Mont Blanc Pens
Nixon
Nooka
Seiko
Tendence
Tissot

Can any one suggest me what approach I should adopt to get in correct order?

任何人都可以建议我应该采用什么方法来获得正确的顺序?

Any help would be greatly appreciated!

任何帮助将不胜感激!

回答by Rich O'Kelly

Assuming you want the elements listed in alphabetical order by their value:

假设您希望元素按其值按字母顺序列出:

var values = [];
for(var i in json.message) {
   values.push(json.message[i]);
}
var str = values.sort().join('\n');

Update

更新

To form an array of key-value pairs ordered by their (string) value:

要形成按(字符串)值排序的键值对数组:

var values = [];
for(var i in json.message) {
   values.push({ key: i, value: json.message[i] });
}
values.sort(function(a, b) { return a.value.localeCompare(b.value); });
var str = values.map(function (kvp) { return kvp.value; }).join('\n');

回答by Sean Kinsey

var message = object.message;
var brands = Object.keys(message)
               .map(function(key) { return message[key]; })
               .sort();

alert(brands.join('\n'));

回答by nikolay naychov

using JS library undescore.js is pretty easy, just do:

使用 JS 库 undescore.js 非常简单,只需执行以下操作:

json.message=_.sortBy(json.message,function(i){return i;})

回答by Elkappo

You can sort it by this way, this is a sort basied on values :

您可以通过这种方式对其进行排序,这是一种基于值的排序:

function sortJsonMap(data){

var values = [];
var keys = [];

for(var i in data) {
   keys.push(i);
   values.push(data[i]);                     
}
values.sort();  
var map = new Object();
for(var i in values) {
     map[keys[i]]=values[i];
}
return map;

}

}

回答by Janath

Add a leading 0 to each index. For example:

为每个索引添加一个前导 0。例如:

var json = { 
    "message": {
        "090": "Adidas", 
        "02": "Armani", 
        "089": "Casio", 
        "01": "Diesel", 
        ...