Skip to content

Instantly share code, notes, and snippets.

@veritstudio
Last active October 19, 2018 10:34
Show Gist options
  • Save veritstudio/ca80d670027805a318a264939e67a55d to your computer and use it in GitHub Desktop.
Save veritstudio/ca80d670027805a318a264939e67a55d to your computer and use it in GitHub Desktop.
Sort array of objects by key value
let coutries = [
{country: 'France', position: 3},
{country: 'Germany', position: 5},
{country: 'Spain', position: 1},
{country: 'Portugal', position: 4},
{country: 'Italy', position: 2}
];
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