This question has already been answered:

I can’t sort the resulting list by 2 fields: NAME and LASTNAME.

Sorting is by NAME only.

How can I fix the situation?

Thank.

// Сортировка значений response.features.sort(function (a, b) { if (a.attributes["NAME"] && b.attributes["NAME"]) { return (a.attributes["NAME"] > b.attributes["NAME"]) ? 1 : -1; } else if (a.attributes["NAME"]) { return -1; } else if (b.attributes["NAME"]) { return 1; } return (a.attributes["LASTNAME"] > b.attributes["LASTNAME"]) ? 1 : -1; }); 

Reported as a duplicate by Qwertiy participants , Grundy , cheops , aleksandr barakin , user194374 Aug 9 '16 at 6:09 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • need to decide on exactly how you want to sour out - Grundy
  • first by NAME, then by LASTNAME - GRM

1 answer 1

Apparently you have everything in the first condition:

 return (a.attributes["NAME"] > b.attributes["NAME"]) ? 1 : -1; 

in which sorting by LASTNAME is not taken into account. But it is easy to fix:

 if (a.attributes["NAME"] && b.attributes["NAME"]) { if (a.attributes["NAME"] == b.attributes["NAME") { return (a.attributes["LASTNAME"] > b.attributes["LASTNAME"]) ? 1 : -1; } return (a.attributes["NAME"] > b.attributes["NAME"]) ? 1 : -1; }