Salve,
sto cercando di ordinare un'array in base alla data più recente in questo modo:
codice:
var defaults = notifications: [
{
id:1,
category: 1,
title: 'abcde',
text: 'abcde',
ago: new Date(y, m, d, h-4, mi)
},
{
id:2,
category: 1,
title: 'abcde',
text: 'abcde',
ago: new Date(y, m, d, h-2, mi)
},
{
id:3,
category: 2,
title: 'abcde',
text: 'abcde',
ago: new Date(y, m, d, h-1, mi)
},
]
Poi tramite un semplice for ho cercato di riordinarlo ma non funziona:
codice:
for(var i = 0;i < defaults.notifications.length; i++){
if(i>0){
if(dateDifference(new Date(),defaults.notifications[i].ago) < dateDifference(new Date(),defaults.notifications[i-1].ago)){
tmp = defaults.notifications[i];
defaults.notifications[i] = defaults.notifications[i-1];
defaults.notifications[i-1] = tmp;
i-=1
}
}
}
la funzione dateDifference mi restituisce i secondi di differenza tra la data attuale e quella della notifica.
Grazie.