The idea is to return an array of strings , sorted by date and time. These strings would be like identifiers. Each value is taken from each of those strings in order to do the corresponding ordering. Now what I am doing is a test in which I have X quantity of identifiers out of order within an array to later process it.
Example:
const string1 = "mi#20221205#es#164509";
const string2 = "mi#20231205#es#164509";
const string3 = "mi#20210523#es#112034";
const string4 = "mi#20220910#es#124055";
const string5 = "mi#20220415#es#143853";
const string6 = "mi#99999999#es#81211";
const string7 = "mi#99999999#es#81212";
const string8 = "mi#20231206#es#164509";
const string9 = "mi#20231206#es#164510";
const res = [
string1,
string2,
string3,
string4,
string5,
string6,
string7,
string8,
string9,
];
You can see that after the first # there are fictitious dates (the 99999999 is an exaggeration on my part). The first thing I did to sort them by date is the following:
const date = [];
for(let a = 0; a < res.length; a++) {
const b = res[a].replaceAll('#', ' ').split(' '); // quito el # y devuelvo un arreglo (length 4)
const e = b.map(f => b.indexOf(f) % 2 !== 0 ? Number(f) : '').filter(Boolean); // me aseguro que los índices que me interesan sean ahora type number y los aparto bajo el criterio indicado
const g = e.shift(); // aparto las fechas
date.push(g);
}
date.sort((a, b) => b - a); // las ordeno
const k = [];
date.map(i => { // compruebo que si existen dentro del arreglo, el valor obtenido sea añadido a un nuevo arreglo
res.map(j => j.includes(i) ? k.push(j) : false);
});
const l = [... new Set(k)]; // elimino duplicados por efecto del proceso anterior
console.log(l);
The result obtained at this point is:
[
'mi#99999999#es#81211',
'mi#99999999#es#81212',
'mi#20231206#es#164509',
'mi#20231206#es#164510',
'mi#20231205#es#164509',
'mi#20221205#es#164509',
'mi#20220910#es#124055',
'mi#20220415#es#143853',
'mi#20210523#es#112034'
]
Now, what is making it difficult for me is to order those dates that are repeated in this new arrangement, but whose times are different. Being that I am interested in having the most recent ones first (with the highest number) and at the same time respecting the previously organized arrangement. This, even though it has countless identifiers and is taken into account in the same way for all cases. Seen another way:
[
'mi#99999999#es#81212',
'mi#99999999#es#81211',
'mi#20231206#es#164510',
'mi#20231206#es#164509',
'mi#20231205#es#164509',
'mi#20221205#es#164509',
'mi#20220910#es#124055',
'mi#20220415#es#143853',
'mi#20210523#es#112034'
]
Being that the ordered cases were:
'mi#99999999#es#81211',
'mi#99999999#es#81212',
'mi#20231206#es#164509',
'mi#20231206#es#164510',
I know that the defined variables are not the most appropriate, but it is a quick test and I did not focus on it. I appreciate your input.
You can make your own sort based on both properties:
I think no proxy processes are needed, it should be enough to sort the array: