JS helpers for Leetcode and friends
const sortObjects = (arr, key, asc = true) => {
return arr.sort((a, b) => {
if (a[key] < b[key]) return asc ? -1 : 1;
if (a[key] > b[key]) return asc ? 1 : -1;
return 0;
});
};/**
* Time complexity: O(nlog(n))
*/
const sort = (arr, asc = true) => {
return arr.sort((a, b) => {
if (a < b) return asc ? -1 : 1;
if (a > b) return asc ? 1 : -1;
return 0;
});
};