Created
August 27, 2019 07:21
-
-
Save vijjusri14/31af5fdf99fbfb2f1acb8c0bdc76c71a to your computer and use it in GitHub Desktop.
Sort js object by key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var cities = [{code:'HYD',city:'Hyderabad'}, | |
| {code:'MUM',city:'Mumbai'}, | |
| {code:'BLR',city:'Bengaluru'}, | |
| {code:'PNQ',city:'Pune'}, | |
| {code:'MAA',city:'Chennai'}]; | |
| //Sort descending: | |
| cities.sort((a, b) => (a.city > b.city ? -1 : 1)); | |
| console.log(cities) | |
| //Sort ascending: | |
| cities.sort((a, b) => (a.city > b.city ? 1 : -1)); | |
| console.log(cities) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment