function __ordenarJSON(data, key, orden) {
return data.sort((a, b) => {
let x = a[key],
y = b[key];
if (orden === 'asc') {
let dateA = new Date(x).getTime();
let dateB = new Date(y).getTime();
return dateA > dateB ? -1 : 1;
}
if (orden === 'desc') {
let dateA = new Date(x).getTime();
let dateB = new Date(y).getTime();
return dateA > dateB ? 1 : -1;
}
});
}
// Usando
let json = [{order : 1, dato : 'Valor 1'}, {order : 2, dato : 'Valor 2'}];
json = __ordenarJSON(json, 'order', 'asc');// Ordenar ascendete
json = __ordenarJSON(json, 'order', 'asc');// Ordenar descendete
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.