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?– Sergio
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
– GeanaDias
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– Sergio
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.
– Isac