N
How you ordered ES6, there are many approaches to achieve what you ask. In this example I show you one of them:const objetoIds = {
'19huH6KOy0EYau0e0Oc6CG': {
S: '15-12-2018'
},
'1Fr3I1pE6IcSEy4s0o0UwW': {
S: '31-12-2018'
},
'1UIrWAsEP6YUyQEiy4WAww': {
S: '31-12-2018'
},
'3extCHS2cg02YCY6GMEkG6': {
S: '31-12-2018'
},
'4eQUYsAKvDYYT7113b3E6J': {
S: '5-4-2019'
},
'4gnGMMruak2qU0yQqy884g': {
S: '15-12-2018'
},
'6lk1HPWKNaY0mWkU0Io6iq': {
S: '31-12-2018'
}
};
// Función para convertir las fechas a formato UTC,
// para poder realizar la comparación de las mismas.
// Es una reutilización de lo propuesto por 'ramirozap'
const convertirFecha = strFecha => {
const fecha = strFecha.split('-');
return Date.UTC(fecha[2], parseInt(fecha[1]) - 1, fecha[0]);
};
// PASO 1
const map = new Map();
Object.keys(objetoIds).map( => {
map.set(, objetoIds[_]['S']);
});
// PASO 2
_map[Symbol.iterator] = function*() {
yield* [...this.entries()].sort(
([, a], [, b]) => convertirFecha(a) - convertirFecha(b)
);
};
// Paso 3a
const mapAsc = [..._map].map(([id, S]) => ({ id, S }));
console.log(mapAsc);
// PASO 3b
const _mapAsc = [..._map].reduce(
(acc, [id, S]) => Object.assign(acc, { [id]: { S } }), {}
);
console.log(_mapAsc);STEP #1:Using https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Map , first convert each of the objects into type values hash, obtaining the format clave => valor. In your specific case, the result is this way:Map {
'19huH6KOy0EYau0e0Oc6CG' => '15-12-2018',
'1Fr3I1pE6IcSEy4s0o0UwW' => '31-12-2018',
'1UIrWAsEP6YUyQEiy4WAww' => '31-12-2018',
'3extCHS2cg02YCY6GMEkG6' => '31-12-2018',
'4eQUYsAKvDYYT7113b3E6J' => '5-4-2019',
'4gnGMMruak2qU0yQqy884g' => '15-12-2018',
'6lk1HPWKNaY0mWkU0Io6iq' => '31-12-2018'
}
STEP #2:They are then used https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Generador taking and changing https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Symbol/iterator of https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Map/@@iterator to obtain an adapted result by ordering the elements by date to return them in the form of arrangement. The result of this step is as follows:console.log([...map]);
[
[ '19huH6KOy0EYau0e0Oc6CG', '15-12-2018' ],
[ '4gnGMMruak2qU0yQqy884g', '15-12-2018' ],
[ '1Fr3I1pE6IcSEy4s0o0UwW', '31-12-2018' ],
[ '1UIrWAsEP6YUyQEiy4WAww', '31-12-2018' ],
[ '3extCHS2cg02YCY6GMEkG6', '31-12-2018' ],
[ '6lk1HPWKNaY0mWkU0Io6iq', '31-12-2018' ],
[ '4eQUYsAKvDYYT7113b3E6J', '5-4-2019' ]
]
It is at this time where you can draw, for example, 2 types of results:STEP #3a: Arrangement of Objects.[
{ id: '19huH6KOy0EYau0e0Oc6CG', S: '15-12-2018' },
{ id: '4gnGMMruak2qU0yQqy884g', S: '15-12-2018' },
{ id: '1Fr3I1pE6IcSEy4s0o0UwW', S: '31-12-2018' },
{ id: '1UIrWAsEP6YUyQEiy4WAww', S: '31-12-2018' },
{ id: '3extCHS2cg02YCY6GMEkG6', S: '31-12-2018' },
{ id: '6lk1HPWKNaY0mWkU0Io6iq', S: '31-12-2018' },
{ id: '4eQUYsAKvDYYT7113b3E6J', S: '5-4-2019' }
]
STEP #3b: Format of objects, how it was originally placed.{
'19huH6KOy0EYau0e0Oc6CG': { S: '15-12-2018' },
'4gnGMMruak2qU0yQqy884g': { S: '15-12-2018' },
'1Fr3I1pE6IcSEy4s0o0UwW': { S: '31-12-2018' },
'1UIrWAsEP6YUyQEiy4WAww': { S: '31-12-2018' },
'3extCHS2cg02YCY6GMEkG6': { S: '31-12-2018' },
'6lk1HPWKNaY0mWkU0Io6iq': { S: '31-12-2018' },
'4eQUYsAKvDYYT7113b3E6J': { S: '5-4-2019' }
}