Problems with sorting an array list

Asked

Viewed 101 times

1

I have the following problem:

I try to sort a list of people containing the flag that identifies the person (student or teacher), the person’s code, the person’s name, the person’s status (active, inactive, blocked) and the status of participation in that room (active, inactive and blocked)all this information inside a vector I call arrayVelhoAux. In this I want to order this vector alphabetically (A,B,C,D,E, ...).

arrayVelhoAux.FLG_IDENT_PESSO = arrayCursor[j].FLG_IDENT_PESSO;
arrayVelhoAux.COD_IDENT_PESSO = pessoa.COD_IDENT_PESSO;
arrayVelhoAux.TXT_NOMEX_PESSO = pessoa.TXT_NOMEX_PESSO;
arrayVelhoAux.FLG_STATU_PESSO = pessoa.FLG_STATU_PESSO;
arrayVelhoAux.FLG_STATU_PARTC = arrayCursor[j].FLG_STATU_PARTC;

arrayVelho.push(arrayVelhoAux);

To fill this vector I am making a loop, because there is data that I picked up in other tables. After the full fill of this vector I make a .sort() with a view to ordering it.

arrayVelho.sort(compareArray);

In function compareArray I simply compare which one is larger. Capitalize all letters so that all letters are compared in the same size.

function compareArray(a1,b1) {
    if(a1.TXT_NOMEX_PESSO.toUpperCase() > b1.TXT_NOMEX_PESSO.toUpperCase()) return 1;
    if(a1.TXT_NOMEX_PESSO.toUpperCase() < b1.TXT_NOMEX_PESSO.toUpperCase()) return -1;
    return 0;
}

Apparently there is nothing wrong, and something interesting happens because, only comparing some, others are out of order. As in the image below.

Problema com ordenação de uma lista de array

I would like to know whether there is a better solution than it is, or whether it would be possible to coordinate and make this solution 100% functional.

  • It would be interesting to create a complete, minimal and verifiable example that displays the problem, including the full entry.

  • Where can I create this example ?

  • Edit out the question. What I mean is that you need to display a code that generates the function input vector, so we can see exactly what’s going on. The problem may be in the input data, for example.

  • No, I actually got to resolve qse here, the problem is that function should returns 1, -1 and 0, in my returned self only if a is greater than b.

  • The problem now is that the ordination has gone from minor to major.

  • Probably change the order of if must solve.

  • It is not worked, I updated the question with the new compare function

  • Check it out now

Show 3 more comments

1 answer

3


Renan, it’s interesting that you return an integer instead of a Boolean.

var nomes = ["Alessandro", "Alessandra", "Alexandre", "Aline", "Antônio", "Breno", "Bruna", "Carlos", "Carla", "Célia", "Cecília", "César", "Danilo", "Dalila", "Deneval", "Eduardo", "Eduarda", "Esther", "Elísio", "Fábio", "Fabrício", "Fabrícia", "Félix", "Felícia", "Feliciano", "Frederico", "Fabiano", "Gustavo", "Guilherme", "Gúbio", "Heitor", "Hélio", "Hugo", "Isabel", "Isabela", "Ígor", "João", "Joana", "Júlio César", "Júlio", "Júlia", "Janaína", "Karla", "Kléber", "Lucas", "Lorena", "Lorraine", "Larissa", "Ladislau", "Marcos", "Meire", "Marcelo", "Marcela", "Margarida", "Mércia", "Márcia", "Marli", "Morgana", "Maria", "Norberto", "Natália", "Nataniel", "Núbia", "Ofélia", "Paulo", "Paula", "Pablo", "Pedro", "Raul", "Rafael", "Rafaela", "Ricardo", "Roberto", "Roberta", "Sílvia", "Sílvia", "Silas", "Suélen", "Sara", "Salvador", "Sirineu", "Talita", "Tertuliano", "Vicente", "Víctor", "Vitória", "Yango", "Yago", "Yuri", "Washington", "Warley"];

//colocar os itens do array em um ordem aleatoria.
var suffle = function (nome1, nome2) {
  return Math.random() * 10;
}

var compare = function (nome1, nome2) {
  if ( nome1 < nome2 )
    return -1;
  if ( nome1 > nome2 )
    return 1;
  return 0;
}
var orderAsc = function(nome1, nome2) {  
  return compare(nome1, nome2) * 1;
}
var orderDesc = function(nome1, nome2) {  
  return compare(nome1, nome2) * -1;
}

nomes.sort(suffle);
console.log(nomes);

nomes.sort(orderAsc);
console.log(nomes);

nomes.sort(suffle);
console.log(nomes);

nomes.sort(orderDesc);
console.log(nomes);

note that in the above case, the name Ígor was not ordered as expected, it appears at the end of the array (orderAsc) or at the beginning (orderDesc);

To solve this problem, you can use Intl.Collator, it will already return an integer as a result of the comparison, and has some additional options that can help you compare strings.

var nomes = ["Alessandro", "Alessandra", "Alexandre", "Aline", "Antônio", "Breno", "Bruna", "Carlos", "Carla", "Célia", "Cecília", "César", "Danilo", "Dalila", "Deneval", "Eduardo", "Eduarda", "Esther", "Elísio", "Fábio", "Fabrício", "Fabrícia", "Félix", "Felícia", "Feliciano", "Frederico", "Fabiano", "Gustavo", "Guilherme", "Gúbio", "Heitor", "Hélio", "Hugo", "Isabel", "Isabela", "Ígor", "João", "Joana", "Júlio César", "Júlio", "Júlia", "Janaína", "Karla", "Kléber", "Lucas", "Lorena", "Lorraine", "Larissa", "Ladislau", "Marcos", "Meire", "Marcelo", "Marcela", "Margarida", "Mércia", "Márcia", "Marli", "Morgana", "Maria", "Norberto", "Natália", "Nataniel", "Núbia", "Ofélia", "Paulo", "Paula", "Pablo", "Pedro", "Raul", "Rafael", "Rafaela", "Ricardo", "Roberto", "Roberta", "Sílvia", "Sílvia", "Silas", "Suélen", "Sara", "Salvador", "Sirineu", "Talita", "Tertuliano", "Vicente", "Víctor", "Vitória", "Yango", "Yago", "Yuri", "Washington", "Warley"];

//colocar os itens do array em um ordem aleatoria.
var suffle = function (nome1, nome2) {
  return Math.random() * 10;
}

var collator = new Intl.Collator("pt-BR", { sensitivity : "base" });
var orderAsc = function(nome1, nome2) {  
  return collator.compare(nome1, nome2) * 1;
}
var orderDesc = function(nome1, nome2) {  
  return collator.compare(nome1, nome2) * -1;
}

nomes.sort(suffle);
console.log(nomes);

nomes.sort(orderAsc);
console.log(nomes);

nomes.sort(suffle);
console.log(nomes);

nomes.sort(orderDesc);
console.log(nomes);

note that I used the option sensitivity : "base", in this case the comparison will be case-insensitive and accent-sensitive, then there is no need to make a .toUpperCase(), and the ordering will present the expected result even if the words have accents.

but as safari still does not support the Intl.Collator (And you still speak ill of IE ;D), so you might need to use a Polyfill: Intl.js

  • Something interesting is happening, my vector is in descending order, how to change it, I want it to begin in A, B, C .. Z .

  • 1

    @Renanrodrigues, I made some changes in the response.

Browser other questions tagged

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