简体   繁体   中英

how to sort dictionary object based on values in d3.js?

I m having object like

 var data ={ "a":10,"b":12,"c":34,"d":78 } 

how to sort this dictionary object based on values in javascript

I need output like

 data ={"d":78,"c":34,"b":12,"a":10,} 

Order of properties in objects in not guaranteed but you can create array of keys with Object.keys() and sort it by values of object.

 var data ={ "a":10,"b":12,"c":34,"d":78 } var result = Object.keys(data).sort(function(a, b) { return data[b] - data[a]; }) console.log(result) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM