Last active
September 24, 2020 14:30
-
-
Save debabrata100/73c635872db04ff6cf7393f3f1d08153 to your computer and use it in GitHub Desktop.
Sort Array of objects with respect to another array
This file contains hidden or 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
function sortFunc(a, b) { | |
var sortingArr = ["A", "B", "C"]; | |
return sortingArr.indexOf(a.type) - sortingArr.indexOf(b.type); | |
} | |
const itemsArray = [ | |
{ | |
type: "A", | |
}, | |
{ | |
type: "C", | |
}, | |
{ | |
type: "B", | |
}, | |
]; | |
console.log(itemsArray); | |
itemsArray.sort(sortFunc); | |
console.log(itemsArray); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment