I'm looking to do a custom sort on an array
based on one of its attributes. So for example if I have a person
object:
var person = {name:""};
and I want to sort on name
but not alphabetically. So if I have:
- Tim
- Mike
- Jen
- Ashley
but I want this to be sorted:
- Mike
- Ashley
- Tim
- Jen
How would I accomplish this? I know I can use a function to compare:
array.sort(function(x, y) {
if (x < y) {
return -1;
}
if (x > y) {
return 1;
}
return 0;
});
But this would result in an alphabetical sort. Then I thought if there was a way to assign a numerical value to each potential name (I will have a known, limited set) then to try that, but I'm not sure it's the correct approach. Any help would be appreciated!