Problems in sorting by more than one criterion

Asked

Viewed 54 times

0

In java we have the possibility to override the compareTo function of a class and use Sort() to sort an object array. I would like to do the same with Javascript, I already know I could use this function call Sort(property) but I am not able to adapt the function below in Sort. I believe it’s because I want to use more than one ordering criterion.

I have a report object, with the properties below: I want to sort by Qualis, and then by year. When I try to call the ordination 2 times, it re-orders for the year and ignores the ordination by Qualis.

var relatorio = {
        ano: '',
        sigla: '',
        veiculo: '',
        qualis: '',
        fator: '',
        titulo: '',
        autores: ''
    };

Function call:

Relatorio.sort(ordenacaoDinamica());

Function I’m trying to:

function teste(){
        var propriedade = 'qualis';
        return function (a,b) {
            if (a[propriedade] < b[propriedade]){
                return -1;
            } 
            if (a[propriedade] > b[propriedade]){
                return 1;
            }
            else{
                propriedade = 'ano';
                if (a[propriedade] > b[propriedade]){
                    return -1;
                } 
                if (a[propriedade] < b[propriedade]){
                    return 1;
                }
            }
            return 0;
        }
    }
  • That property qualis is numerical or text?

  • is text, the ordering is working, but when trying to sort within equal Qualis, it goes wrong... it re-sorts the whole array and ignores the previous ordering

  • 1

    But if it’s text to use < and > won’t sort the way it should be. Can you give an example array(?) with 4 or 5 elements to build a demo

  • I suggest you do as Sergio said and set an example with the ordination that you say does not work, so that we can see, test and respond.

2 answers

1

Similar to the way you were doing with the if, you can check if the qualis is the same as the other if it is you check the year.

In javascript you can enjoy that value 0 is interpreted as false to do:

let ordenacao = comparacaoDoQualis || comparacaoDoAno;

Follow an example of the function to sort:

let relatorio = [
    {'ano': '2020', 'qualis': 'B2'},
    {'ano': '2017', 'qualis': 'A2'},
    {'ano': '2017', 'qualis': 'B2'},
    {'ano': '2018', 'qualis': 'A3'},
    {'ano': '2017', 'qualis': 'A3'},
    {'ano': '2018', 'qualis': 'B1'},
    {'ano': '2015', 'qualis': 'B2'},
];


relatorio.sort((a, b) => {
   let qualis = (a.qualis == b.qualis) ? 0 : ((a.qualis > b.qualis) ? 1 : -1);
   let ano = (a.ano == b.ano) ? 0 : ((a.ano > b.ano) ? 1 : -1);

   //se o qualis for 0 ele retorna o valor da comparação do ano
   return qualis || ano;
});

console.log(relatorio);

0

Try it like this:

const data = [
    { qualis: 'A1', ano: '2017' },
    { qualis: 'A1', ano: '2014' },
    { qualis: 'A2', ano: '2011' },
    { qualis: 'A3', ano: '2012' },
    { qualis: 'A6', ano: '2019' },
    { qualis: 'A9', ano: '2020' },
];

function sortData(data) {
  return data.slice().sort((o1, o2) => {
    const qualis = o1.qualis.localeCompare(o2.qualis);
    
    if (qualis !== 0) {
      return qualis;
    }
    
    const ano = o1.ano.localeCompare(o2.ano);
    
    return ano;
  });
}

const sortedData = sortData(data);

console.log(sortedData);

Browser other questions tagged

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