0

I am sorting my people data and works fine

people.sort(
  (a, b) =>
  a.nickname.localeCompare(b.nickname) ||
  a.name.localeCompare(b.name) ||
  a.age - b.age
);

The problem is that some nickname are empty string '' and they go on top of the array, which I don't want

How can I sort them just by name and age when the nickname is empty string? I am not able to use if statements

asfafafags
  • 99
  • 5
  • Have you ever tried something like this? people.sort( (a, b) => { return (a.nickname !== '' && b.nickname !== '') ? (a.nickname.localeCompare(b.nickname) || a.name.localeCompare(b.name) || a.age - b.age) : (a.name.localeCompare(b.name) || b.age - a.age); } ); – Cristóvão Fábio Sep 14 '22 at 15:58

0 Answers0