Created
May 15, 2020 02:25
-
-
Save AndrewBuntsev/747a33e7cd2436f51c18b36be76d038e to your computer and use it in GitHub Desktop.
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
var sortBy = function(arr, ...sortByArgs){ | |
arr.sort(function(a,b){ | |
var sortResult = 0; | |
sortByArgs.forEach(function(arg){ | |
if (sortResult != 0) return; | |
if(Object.values(arg)[0] == 'desc'){ | |
var propName = Object.keys(arg)[0]; | |
if (a[propName] > b[propName]){ | |
sortResult = -1; | |
return; | |
} | |
if (a[propName] < b[propName]) { | |
sortResult = 1; | |
return; | |
} | |
} | |
else { | |
if (a[arg] < b[arg]){ | |
sortResult = -1; | |
return; | |
} | |
if (a[arg] > b[arg]) { | |
sortResult = 1; | |
return; | |
} | |
} | |
}); | |
return sortResult; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment