Skip to content

Instantly share code, notes, and snippets.

@tejasrsuthar
Created June 22, 2020 05:08
Show Gist options
  • Save tejasrsuthar/a16803f627baa30c18322bbe297499f8 to your computer and use it in GitHub Desktop.
Save tejasrsuthar/a16803f627baa30c18322bbe297499f8 to your computer and use it in GitHub Desktop.
Normal Array of Objects sort by
const usCityOptions = cities
// Object sorting by key
.sort((a, b) => {
if(a.state < b.state){
return -1;
// a should come after b in the sorted order
}else if(a.state > b.state){
return 1;
// and and b are the same
}else{
return 0;
}
})
.map((o) => <option key={o.state} value={o.state}>{o.state}</option>);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment