Help ordering Javascript fix

0

I have a very simple arrangement of objects, something like this:

let profiles = [
  {
    'title': 'X',
    'url' : 'https://google.com',
    'description': 'Lorem'
  },
  {
    'title': 'Y',
    'description': 'Ipsum'
  }
];

Each object has the properties title and description , but optionally has url .

I make a sort to the arrangement to sort it in alphabetical order according to title .

profiles.sort( (a, b) => {
  return a.title.toLowerCase() > b.title.toLowerCase();
});

And at the end I insert the data of the fix in the HTML with a forEach :

profiles.forEach( (profile) => {
  let element = '<tr><td>${profile.title}</td><td>${profile.description}</td></tr>';
  if ( profile.url !== undefined ) {
    element = '<tr><td>${profile.title}</td><td><a href="${profile.url}" target="_blank">${profile.description}</a></td></tr>';
  }
  document.getElementsByTagName('tbody')[0].insertAdjacentHTML('beforeend', element);
});

This works well, the problem is that when I get to the 11 elements of the arrangement it does not order it correctly. Example:

I should get this:

Age
Digimon
Github
Hobbies
Job
MAL
Name
OS
Steam
Twitter
Type

But I get this:

Job
OS
Github
Digimon
Name
MAL
Hobbies
Age
Steam
Twitter
Type

So far I have tried to put both the declaration of the array and the sort within a function that returns a promise, and then use the then of that promise and thus insert in the HTML, but it worked exactly the same. It looked something like this:

let ordenar = () => {
  return new Promise( (resolve, reject) => {

    let profiles = [
      {..},{..}
    ];

    profiles.sort( (a, b) => {
      return a.title.toLowerCase() > b.title.toLowerCase();
    });

    resolve(profiles);

  });
}

ordenar().then(resp => {
  resp.forEach( (profile) => {
    ....
  });
}).catch();
    
asked by akko 17.08.2018 в 07:33
source

2 answers

2

The sort function of the arrays does not expect a boolean in the comparison, but a -1, 0 or 1.

The official documentation says it has to be this way or if does not specify the following: Si se retornan resultados inconsistentes entonces el orden de ordenamiento es indefinido.

You will have to do the sort function with a comparison of the type:

profiles.sort(function (a, b) {
  if (a.title > b.title) {
    return 1;
  }
  if (a.title < b.title) {
    return -1;
  }

  return 0;
});
    
answered by 17.08.2018 / 08:22
source
0

I just wanted to add that to also order special characters, such as accents and eñes, common in Spanish, we can use localCompare and pass the flags of 'is' (Spanish) and {sensitivity: 'base'} that makes á == a for example.

let profiles = [{'title': 'Az'},{'title': 'Áb'},{'title': 'ay'},{'title': 'Ña'},{'title': 'Nz'},{'title': 'ny'}];


profiles.sort((a, b) => a.title.localeCompare(b.title, 'es', {sensitivity: 'base'}))


console.log(profiles)

Some browsers or JavaScript engines may not accept it, or have bugs. For example, nodejs v8.11.4 supports it but does not execute it, so it is probably a bug of that V8 version.

    
answered by 17.08.2018 в 14:42