简体   繁体   中英

Javascript sort array of multiple values in one index

I have an array with values

[[300, 2.6], [400, 2.2], [500, 3.0]]

How can I sort both dimensions such that I can display the output correctly in a graph?
array.sort() does not seem to work as it only sorts by one value in the entire array.

Thank you.

Your best shot is to use lodash :

 var values = [[500, 1.1], [300, 2.2], [400, 1.5]]; var sorted = _.sortBy(values, function(pair){return pair[0];}); console.log('sorted values: ' + JSON.stringify(sorted));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>

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