Sorting objects in Javascript

Asked

Viewed 93 times

1

Good evening, I’m having a difficult to sort array in javascript, the problem is that I need to sort it first by number of characters, ( Words with more characters appears first), I’ve done that, but after that I need to sort the words that have the same number of characters in alphabetical order but I can’t do it without disrespecting the first criterion.

follows My code :

var items = [
    { name: 'one', value: 3 },
    { name: 'three', value: 5 },
    { name: 'mond', value: 4 },
    { name: 'four', value: 4 },
    { name: 'ajdh', value: 4 },
    { name: 'at', value: 2 },
    { name: 'midnight', value: 8 }
  ];
  items.sort(function (a, b) {
    if (a.value > b.value) {
      return -1;
    }
    if (a.value < b.value) {
      return 1;
    }
    return 0;
  });

  items.sort(function (a, b) {
    if (a.value == b.value) {
        if (a.name > b.name) {
            return -1;
          }
          if (a.name < b.name) {
            return 1;
          }
        }
    return 0;
  });
  console.log(items)

I need him to leave like this:

[ { name: 'midnight', value: 8 },
  { name: 'three', value: 5 },
  { name: 'ajdh', value: 4 },
  { name: 'four', value: 4 },
  { name: 'mond', value: 4 },
  { name: 'one', value: 3 },
  { name: 'at', value: 2 } ]

Note that when property value(Represents the number of characters) repeats with the same value the array needs to be in alphabetical order, but I don’t know how to do this without messing up the remaining order. Thank you

1 answer

3


You don’t need to use two calls to the method sort, just compare the length of the words with a if, if they have the same length, return the function according to the word order, otherwise return according to the string length.

Example (using ternary operator):

var items = [
  { name: 'one', value: 3 },
  { name: 'three', value: 5 },
  { name: 'mond', value: 4 },
  { name: 'four', value: 4 },
  { name: 'ajdh', value: 4 },
  { name: 'at', value: 2 },
  { name: 'midnight', value: 8 }
];

items.sort((a, b) =>
  // se a quantidade de caracteres for igual...
  a.value === b.value ?
  // ordena por ordem alfabética crescente
  a.name.localeCompare(b.name, 'pt-BR') :
  // senão ordena pelo comprimento da palavra decrescente
  b.value - a.value
);

console.log(items);

But your return logic 0 to preserve the order of the words sorted by size also works, you just have to change the order in which the words are being sorted alphabetically; return 1 when a > b, and -1 when a < b (or use localeCompare as in the example above):

var items = [
  { name: 'one', value: 3 },
  { name: 'three', value: 5 },
  { name: 'mond', value: 4 },
  { name: 'four', value: 4 },
  { name: 'ajdh', value: 4 },
  { name: 'at', value: 2 },
  { name: 'midnight', value: 8 }
];

items.sort((a, b) => b.value - a.value);

items.sort((a, b) => {
  if (a.value === b.value) {
    if (a.name > b.name) return 1;
    if (a.name < b.name) return -1;
  }
  return 0;
});

console.log(items);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.