I have an object called report and I have a report vector and need to do several sorts, so I thought I would do a sorting function that was "universal". I wanted to be able to pass the attribute to be sorted, so that I could reuse the sort 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: When calling:
ordena(vetor,'ano');
I should have the vector ordered by year
When calling:
ordena(vetor,'titulo');
I should have the vector ordered by title
Is it possible? Or do I have to do a specific sort function for each attribute?