Skip to content

Instantly share code, notes, and snippets.

@dested

dested/sort-bools.js

Created Jan 21, 2019
Embed
What would you like to do?
const m = [
{firstOrder: false, secondOrder: false, thirdOrder: false, rank: 10},
{firstOrder: false, secondOrder: true, thirdOrder: false, rank: 15},
{firstOrder: false, secondOrder: false, thirdOrder: false, rank: 12},
{firstOrder: false, secondOrder: false, thirdOrder: false, rank: 16},
{firstOrder: false, secondOrder: false, thirdOrder: false, rank: 21},
{firstOrder: false, secondOrder: false, thirdOrder: false, rank: 54},
{firstOrder: false, secondOrder: false, thirdOrder: false, rank: 13},
{firstOrder: false, secondOrder: false, thirdOrder: false, rank: 30},
{firstOrder: true, secondOrder: false, thirdOrder: false, rank: 35},
{firstOrder: false, secondOrder: false, thirdOrder: true, rank: 16},
{firstOrder: false, secondOrder: false, thirdOrder: false, rank: 7},
];
m.sort((a, b) =>
b.firstOrder - a.firstOrder ||
b.secondOrder - a.secondOrder ||
b.thirdOrder - a.thirdOrder ||
a.rank - b.rank,
);
/*
m = [
{ firstOrder: true,
secondOrder: false,
thirdOrder: false,
rank: 35 },
{ firstOrder: false,
secondOrder: true,
thirdOrder: false,
rank: 15 },
{ firstOrder: false,
secondOrder: false,
thirdOrder: true,
rank: 16 },
{ firstOrder: false,
secondOrder: false,
thirdOrder: false,
rank: 7 },
{ firstOrder: false,
secondOrder: false,
thirdOrder: false,
rank: 10 },
{ firstOrder: false,
secondOrder: false,
thirdOrder: false,
rank: 12 },
{ firstOrder: false,
secondOrder: false,
thirdOrder: false,
rank: 13 },
{ firstOrder: false,
secondOrder: false,
thirdOrder: false,
rank: 16 },
{ firstOrder: false,
secondOrder: false,
thirdOrder: false,
rank: 21 },
{ firstOrder: false,
secondOrder: false,
thirdOrder: false,
rank: 30 },
{ firstOrder: false,
secondOrder: false,
thirdOrder: false,
rank: 54 }
]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment