Sorting an array by multiple criteria with vanilla JavaScript | Go Make Things

PHOTO EMBED

Mon May 02 2022 18:31:17 GMT+0000 (Coordinated Universal Time)

Saved by @arielvol

var votes = [
	{ title: 'Apple', votes: 1 },
	{ title: 'Milk', votes: 2 },
	{ title: 'Carrot', votes: 3 },
	{ title: 'Banana', votes: 2 }
];

votes.sort(function (vote1, vote2) {
	console.log(vote1);
	console.log(vote2);
});

// Logs this to the console...
// {title: "Apple", votes: 1}
// {title: "Milk", votes: 2}
// {title: "Milk", votes: 2}
// {title: "Carrot", votes: 3}
// {title: "Carrot", votes: 3}
// {title: "Banana", votes: 2}
content_copyCOPY

https://gomakethings.com/sorting-an-array-by-multiple-criteria-with-vanilla-javascript/