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.
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>');
Pretty straight-forward.
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 > b.name;
});
console.log(data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With