Last active
October 19, 2018 10:34
-
-
Save veritstudio/ca80d670027805a318a264939e67a55d to your computer and use it in GitHub Desktop.
Sort array of objects by key value
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
let coutries = [ | |
{country: 'France', position: 3}, | |
{country: 'Germany', position: 5}, | |
{country: 'Spain', position: 1}, | |
{country: 'Portugal', position: 4}, | |
{country: 'Italy', position: 2} | |
]; |
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
let coutries = [ | |
{country: 'France', position: 3}, | |
{country: 'Germany', position: 5}, | |
{country: 'Spain', position: 1}, | |
{country: 'Portugal', position: 4}, | |
{country: 'Italy', position: 2} | |
]; | |
//sort ascending by position value | |
countries.sort((a, b) => { return a.position - b.position }); | |
//sort descending by position value | |
countries.sort((a, b) => { return b.position - a.position }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment