In java we have the possibility to overwrite the compareTo function of a class and use sort () to sort a vector of objects. I would like to do the same with JavaScript, I already know I could use this sort function call but I am not able to adapt the function below in the sort. I think it's because I want to use more than one sorting criterion.
I have an object of the report type, with the following properties: I want to order by qualis, and then by year. When I try to call the sort order 2 times, it re-sorts for the year and ignores the sort order.
var relatorio = {
ano: '',
sigla: '',
veiculo: '',
qualis: '',
fator: '',
titulo: '',
autores: ''
};
Function call:
Relatorio.sort(ordenacaoDinamica());
Function I'm trying:
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;
}
}