Skip to content

Instantly share code, notes, and snippets.

@geomago
Last active June 19, 2020 05:47
Show Gist options
  • Save geomago/7183678558075a199076a2743769f713 to your computer and use it in GitHub Desktop.
Save geomago/7183678558075a199076a2743769f713 to your computer and use it in GitHub Desktop.
sort_by_2_cols
cars.sort( (a,b) => a.make < b.make ? -1
: a.make > b.make ? 1
: a.price > b.price ? -1
: a.price < b.price ? 1
: 0 );
// cars is now:
[
{id:4,make:"Bugatti",model:"Chiron Pur Sport","price":3000000,colour:"blue"},
{id:8,make:"Bugatti",model:"Chiron Pur Sport","price":3000000,colour:"red"},
{id:1,make:"Ferrari",model:"812GTS","price":336000,colour:"rosso corsa"},
{id:1,make:"Ferrari",model:"812GTS","price":336000,colour:"rosso corsa"},
{id:2,make:"Ferrari",model:"F8 Spider","price":262000,colour:"giallo modena"},
{id:6,make:"Ferrari",model:"F8 Spider","price":262000,colour:"giallo modena"},
{id:7,make:"Ferrari",model:"F8 Spider","price":262000,colour:"red"},
{id:3,make:"Lamborghini",model:"Aventador S","price":329400,colour:"blu le mans"},
{id:5,make:"McLaren",model:"New GT","price":203000,colour:"helios orange"}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment