Skip to content

Instantly share code, notes, and snippets.

@alaztetik
Created January 6, 2021 05:08
Show Gist options
  • Save alaztetik/8e9c090fcabc775880babcfa64150077 to your computer and use it in GitHub Desktop.
Save alaztetik/8e9c090fcabc775880babcfa64150077 to your computer and use it in GitHub Desktop.
A function that will sort an array of objects according to primary and secondary properties in order
const sortArrayObjs = function(arr, prop1, prop2) {
let sort1 = [...arr].sort((a, b) => {
if (a[prop1] == b[prop1]) {
if (a[prop2] === b[prop2]) return 0;
return (a[prop2] < b[prop2]) ? -1 : 1;
} else {
return (a[prop1] < b[prop1]) ? -1 : 1;
}
});
return sort1;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment