Created
January 6, 2021 05:08
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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