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

时间:2020-12-15 15:56:01

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尼克松汉密尔顿Luminox DKNY化石卡西欧阿迪达斯Tendence精工Michael Kors Nooka Mont Blanc钢笔

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卡西欧柴油DKNY Fossil Hamilton Luminox Michael Kors勃朗峰钢笔Nixon Nooka精工Tendence天梭

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

任何人都可以建议我采取什么方法来达到正确的顺序?

Any help would be greatly appreciated!

任何帮助将不胜感激!

5 个解决方案

#1


8  

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');

#2


1  

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

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

#3


0  

Add a leading 0 to each index. For example:

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

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

#4


0  

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

使用JS库unexcore.js非常简单,只需:

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

#5


0  

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;

}

#1


8  

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');

#2


1  

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

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

#3


0  

Add a leading 0 to each index. For example:

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

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

#4


0  

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

使用JS库unexcore.js非常简单,只需:

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

#5


0  

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;

}