1
I have an object called report and I have a report vector and I need to do several sorts, so I thought I would do an ordering function that was "universal". I wanted to be able to pass the attribute to be ordered so that I could reuse the sorting function several times.
Object:
var relatorio = {
ano: '',
sigla: '',
veiculo: '',
qualis: '',
fator: '',
titulo: '',
autores: ''
};
Sort function:
function ordena(vetor,attr) {
vetor.sort(function(a,b) {
return a.attr.localeCompare(b.attr);
});
}
Example: By calling:
ordena(vetor,'ano');
I should have as a result the vector ordered by the year
By calling:
ordena(vetor,'titulo');
I should have as a result the vector ordered by the title
Is it possible? Or do I have to do a specific sorting function for each attribute?
In this case, "vector" in
ordena(vetor,'ano');
would be "report"?– Sam