const data = {"ADF_3":"","ADF_2":"","ADF_1":"","ADF_7":"","ADF_17":"","ADF_6":"","ADF_18":"","ADF_5":"","ADF_15":"","ADF_4":"","ADF_16":"","ADF_9":"","ADF_19":"","ADF_8":"","ADF_20":"","ADF_10":"","ADF_13":"","ADF_14":"","ADF_11":"","ADF_12":""};
{"ADF_1":"","ADF_2":"","ADF_3":"","ADF_4":"","ADF_5":"","ADF_6":"","ADF_7":"","ADF_8":"","ADF_9":"","ADF_10":"","ADF_11":"","ADF12":"","ADF_13":"","ADF_14":"","ADF_15":"","ADF_16":"","ADF_17":"","ADF_18":"","ADF_19":"","ADF_20":""};
Asked
Active
Viewed 49 times
0
-
care to explain or give more details about your question and format that into code while at it? – Chris G Feb 14 '23 at 17:16
-
https://stackoverflow.com/questions/9658690/is-there-a-way-to-sort-order-keys-in-javascript-objects <- here is a case like yours – poPaTheGuru Feb 14 '23 at 17:17
-
Please follow the link. https://stackoverflow.com/questions/9658690/is-there-a-way-to-sort-order-keys-in-javascript-objects – Feb 14 '23 at 17:19
2 Answers
1
const data = {"ADF_3":"","ADF_2":"","ADF_1":"","ADF_7":"","ADF_17":"","ADF_6":"","ADF_18":"","ADF_5":"","ADF_15":"","ADF_4":"","ADF_16":"","ADF_9":"","ADF_19":"","ADF_8":"","ADF_20":"","ADF_10":"","ADF_13":"","ADF_14":"","ADF_11":"","ADF_12":""};
console.log(Object.fromEntries(Object.entries(data).sort(([a],[b])=>
a.localeCompare(b,undefined,{numeric: true}))))

Andrew Parks
- 6,358
- 2
- 12
- 27
0
const data = {"ADF_3":"","ADF_2":"","ADF_1":"","ADF_7":"","ADF_17":"","ADF_6":"","ADF_18":"","ADF_5":"","ADF_15":"","ADF_4":"","ADF_16":"","ADF_9":"","ADF_19":"","ADF_8":"","ADF_20":"","ADF_10":"","ADF_13":"","ADF_14":"","ADF_11":"","ADF_12":""};
const sortedData = {};
Object.keys(data).sort((a, b) => parseInt(a.replace("ADF_", "")) - parseInt(b.replace("ADF_", "")))
.forEach(key => sortedData[key] = data[key]);
console.log(sortedData);

GomuGomu
- 304
- 2
- 9
-
Worth reading https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order/38218582#38218582 – Konrad Feb 14 '23 at 17:23