I am using the library lodash
in Angular
to group this data:
data =
[
{
hash: "13m49k9pg70b",
fecha_registro: "2021-10-26T20:28:25.350000Z",
id: 7791,
nombre: "aprueba"
},
{
hash: "13m49k9pg70b",
fecha_registro: "2021-10-26T20:28:25.350000Z",
id: 7792,
nombre: "aprueba"
},
{
hash: "1sd345612se",
fecha_registro: "2021-11-08T21:22:25.350000Z",
id: 7793,
nombre: "btest"
},
{
hash: "1sd345612se",
fecha_registro: "2021-11-08T21:22:25.350000Z",
id: 7794,
nombre: "btest"
},
{
hash: "asd1w345611s",
fecha_registro: "2021-08-11T10:22:21.350000Z",
id: 7794,
nombre: "cvtest"
},
]
What I do is group the data by the properties nombre
, hash
, fecha_registro
to do this in a function I do this:
this.confPersoDocenteClas = _.groupBy(data, (item) => {
return [ item['nombre'], item['hash'], item['fecha_registro']];
});
console.log("DATA AGRUPADA", this.confPersoDocenteClas)
Which returns me correctly grouped data
aprueba,13m49k9pg70b,2021-10-26T20:28:25.350000Z: [{...}, {...}]
btest, 1sd345612se, 2021-11-08T21:22:25.350000Z: [{...}, {...}]
cvtest, asd1w345611s, 2021-11-08T10:12:21.350000Z: [{...}, {...}]
The "problem" I have is that it returns the data in alphabetical order of the key nombre
, and what I need is that it returns me ordered by fecha_registro
, from the most recent to the oldest, in this way:
btest, 1sd345612se, 2021-11-08T21:22:25.350000Z: [{...}, {...}]
cvtest, asd1w345611s, 2021-11-08T10:12:21.350000Z: [{...}, {...}]
aprueba,13m49k9pg70b,2021-10-26T20:28:25.350000Z: [{...}, {...}]
To try this I have added a line to the function indicated above in this way:
let datos = _.orderBy(data, ['fecha_registro'], ['desc']); //linea agregada
this.confPersoDocenteClas = _.groupBy(datos, (item) => {
return [ item['nombre'], item['hash'], item['fecha_registro']];
});
This sorts them but doing the grouping again sorts them back in alphabetical order, in this case by key nombre
. How can I make it return ordered by fecha_registro
? I hope you can help me, thanks in advance.