Bueno, tengo un problema al ordenar esta tabla por fecha. La función de clasificación como determinante de qué tipo deberíamos usar funciona, pero la función de clasificación ya no funciona y no sé por qué.
html:
<th @click = "sort('data_produktu')" class="date">Data</th>datos:
messages: [], currentSort:'data_produktu', currentSortDir:'asc',métodos:
sort: function(s){ if(s === this.currentSort) { this.currentSortDir = this.currentSortDir==='asc'?'desc':'asc'; } this.currentSort = s; console.log(this.messages); }calculado:
sortedMessages:function() { return this.messages.sort((a,b) => { let modifier = 1; if(this.currentSortDir === 'desc') modifier = -1; if(a[this.currentSort] < b[this.currentSort]) return -1 * modifier; if(a[this.currentSort] > b[this.currentSort]) return 1 * modifier; return 0; }); }Array (salida de console.log porque lo estoy obteniendo de DB):
0: {id_komunikat: '4', data_produktu: '2022-08-12 20:05:30', tresc: 'Example info', typ: '2', status_komunikatu: '1', …} 1: {id_komunikat: '5', data_produktu: '2022-08-12 20:05:36', tresc: 'Example info', typ: '2', status_komunikatu: '1', …} 2: {id_komunikat: '6', data_produktu: '2022-08-12 20:05:39', tresc: 'Example info', typ: '2', status_komunikatu: '1', …} 3: {id_komunikat: '7', data_produktu: '2022-08-12 20:05:47', tresc: 'Example info', typ: '1', status_komunikatu: '1', …}Veo que data_produktu es una date , la date de clasificación no se puede ordenar, con mayor que > o menor que < , pero por marcas de tiempo de resta.
Ex:
array.sort(function(a,b){ return new Date(b) - new Date(a); });La función calculada me parece sospechosa. Probablemente causaría un bucle infinito, porque también muta this.messages in situ invocando el método .sort() . Sugiero cambiarlo a:
sortedMessages:function() { // use `.slice()` to clone first this.messages.slice().sort((a, b) => ...)