Tuesday, May 28, 2024
 Popular · Latest · Hot · Upcoming
77
rated 0 times [  78] [ 1]  / answers: 1 / hits: 109211  / 8 Years ago, tue, february 23, 2016, 12:00:00

how I can sort my json file by name -> value? I want to show by asc value of tag name. Any idea how to convert my json file?



Here is how look my JSON file:



[{
id: 105,
name: FIAT,
active: true,
parentId: 1
}, {
id: 106,
name: AUDI,
active: true,
parentId: 1
}, {
id: 107,
name: BMW,
active: true,
parentId: 1
}, {
id: 109,
name: RENAULT,
active: true,
parentId: 1
}]


I will be grateful if someone help me. Thanks in advance.


More From » php

 Answers
15

I suggest to use Array#sort()





var data = [
{ id: 105, name: FIAT, active: true, parentId: 1 },
{ id: 106, name: AUDI, active: true, parentId: 1 },
{ id: 107, name: BMW, active: true, parentId: 1 },
{ id: 109, name: RENAULT, active: true, parentId: 1 }
];

data.sort(function (a, b) {
return a.name.localeCompare(b.name);
});

document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');




[#63206] Sunday, February 21, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leslijessalyng

Total Points: 650
Total Questions: 85
Total Answers: 109

Location: Croatia
Member since Mon, Sep 6, 2021
3 Years ago
leslijessalyng questions
Fri, Feb 21, 20, 00:00, 4 Years ago
Tue, Jul 30, 19, 00:00, 5 Years ago
Fri, Jul 5, 19, 00:00, 5 Years ago
Wed, Mar 13, 19, 00:00, 5 Years ago
;