I have the following structure:
var participant0 = {
name : "",
nickname : "",
number "99999"
} ;
var participant1 = {
name : "bbb",
nickname : "",
} ;
var participant2 = {
name : "",
nickname : "aaa"
} ;
var participant3 = {
name : "ccc",
nickname : ""
} ;
And I have an array containing instances of the structure:
var array = [participant0, participant3, participant1, participant2];
I would like to sort this array alphabetically. First by name, second by nickname. If these two keys do not exist, I would like to check the key number and put this item at the end of the sort list.
Expected Result:
var array = [participant2, participant1, participant3, participant0];
(To sort an object by "aaa", "bbb", "ccc", "9999")
The following code works fine for sorting by name or alias, but I donβt know how to put an item at the end of the sort list if there is a numeric key:
fav_list.sort(function(x, y) {
return (x.participant.name || x.participant.nickname).localeCompare(y.participant.name || y.participant.nickname);
});