I'd like to sort an array in a customized way.
Example would be a student's classlevel:
_.sortBy(["Junior","Senior","Freshman","Sophomore"], function(classlevel){
// ??
})
Ideally, the sort should return:
["Freshman","Sophomore","Junior","Senior"]
I'm thinking if I could pre-rank the classlevels ahead of time like this:
var classlevelRanked = [{ class: "Junior",
rank: 3
},{ class: "Senior",
rank: 4
},{ class: "Freshman",
rank: 1
},{ class: "Sophomore",
rank: 2
}]
and then apply a sort via:
_.sortBy(classlevelRanked, function(classlevel){
return classlevel.rank;
})
But then I have to strip out the ranks by doing:
.map(function(classlevel){
return classlevel["class"];
})
Is there a more direct way to do this without pre-ranking the classlevels and then stripping it out afterwards?
Thank you.