2
I have the following array:
let datas = ["nov/2018", "set/2018", "jan/2019", "dez/2018", "out/2018"]
when executed
datas.sort()
it orders by alphabetical order, however, I must order first by year and then by alphabetical order.
["dez/2018", "jan/2019", "nov/2018", "out/2018", "set/2018"]
Testing today I arrived at the following line of code:
var datas = ["mar", "abr", "jan", "dez", "set", "mai", "jun", "out", "jul"];
var datas_corretas = ["jan", "fev", "mar", "abr", "mai", "jun", "jul", "ago", "set", "out", "nov", "dez"];
var result = [];
datas_corretas.forEach(function ordenar(element, index){
var mes = datas.filter(function(valor){
return valor == datas_corretas[index];
});
result.push(mes[0]);
for(element of result){
if (element === undefined || element === null){
result.pop(element);
}
}
});
console.log(result);
That way I can sort the data, but the problem is when I use with the year
var datas = ["mar/2018", "abr/2018", "jan/2019", "dez/2018", "set/2018", "mai/2018", "jun/2018", "out/2018", "jul/2018"];
Does anyone have any idea how I can fix this?
I also think it is not the best way to store dates, and I also went to see that this is not only a reusable code, because I tested put the month of
abr/2018
, and he stood in front ofjan/2018
– Raphael Melo De Lima
@Raphaelmelodelima Isn’t alphabetically the months ? Alphabetically it’s like this in my answer. Is it by month order = January equals to 1 and so on ? If that is so then you have not expressed yourself correctly in the question, nor have you read my answer carefully. But if it is, just say that I edit the answer
– Isac
@Raphaelmelodelima I’ve already edited the answer anyway, so at this point it has both versions.
– Isac
I really expressed myself in a wrong way, I apologize for that and thank you very much for your attention
– Raphael Melo De Lima