I want to sort an array of objects with labels, for example
var arrayToBeSorted = [{label: 'firstLabel', value: 123}, {label: 'secondLabel', value: 456}, {label: 'thirdLabel', value: 789}]
and an array of labels as a baseline, for example
var labels = ['secondLabel', 'thirdLabel', 'fourthLabel', 'firstLabel']
Now I want to sort my first array, so that the objects in it follow the order of labels in the second array labels
.
I know basic sorting mechanism with custom comparators like
CustomComparator: function(a, b) {
if (a[0].length > b[0].length) return -1;
if (a[0].length < b[0].length) return 1;
return 0;
}
but I have no idea on how to convert this.
In my research I found this solution on stackoverflow coded in ruby, but I don't know if there is a similar option in javascript.
I appreciate any help, invested quite some time on this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…