Chartjs-Collect json disordered, order it and show it in chart.js

0👍

Assuming the dates are always in the format YYYY-MM-DD as shown in your example, you can sort them alphabetically like this:

const rates = {
   "2018-05-04": {"USD": 1.1969},
   "2018-08-27": {"USD": 1.1633},
   "2018-06-08": {"USD": 1.1754},
   "2018-08-22": {"USD": 1.1616},
   "2018-07-19": {"USD": 1.1588}
};
console.log(JSON.stringify(rates));

const sorted = {};
Object.keys(rates).sort().forEach(key => {
  sorted[key] = rates[key];
});

console.log(JSON.stringify(sorted));

//EDIT:
//In case you only want the values without the dates in an array:

const sortedValues = [];
let i = 0;
Object.keys(rates).sort().forEach(key => {
  sortedValues.push(rates[key].USD);
});

console.log(sortedValues);

Leave a comment