Skip to content

Instantly share code, notes, and snippets.

@debabrata100
Last active September 24, 2020 14:30
Show Gist options
  • Save debabrata100/73c635872db04ff6cf7393f3f1d08153 to your computer and use it in GitHub Desktop.
Save debabrata100/73c635872db04ff6cf7393f3f1d08153 to your computer and use it in GitHub Desktop.
Sort Array of objects with respect to another array
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