I am trying to order this that use key, values. Both are floating numbers.
{
'2023.4': '665999.91',
'2023.5': '1228000.0',
'2023.6': '643842.86',
'2023.7': '510166.67',
'2023.8': '727600.0',
'2023.9': '535327.63',
'2024.1': '472591.88',
'2024.2': '525736.46',
'2024.3': '596570.0',
'2024.4': '548539.52',
'2023.10': '562607.69'
}
My solution is not working and I hit a wall on this one…
// Convert object to array of objects
const dataArray = Object.entries(trends).map(([key, value]) => ({ [key]: value }));
// Sort the array based on the formatted keys
dataArray.sort((a, b) => {
const keyA = Object.keys(a)[0];
const keyB = Object.keys(b)[0];
const [yearA, monthA] = keyA.split('.').map(part => parseInt(part));
const [yearB, monthB] = keyB.split('.').map(part => parseInt(part));
if (yearA === yearB) {
return monthA - monthB;
} else {
return yearA - yearB;
}
});
// Reassign the sorted values back to the original object
dataArray.forEach((item,`your text` index) => {
const key = Object.keys(item)[0];
trends[key] = Object.values(item)[0];
});
console.log(data)
Order a javascript dict – array