john_js_py
john_js_py

Reputation: 9

How to sort an object by date in keys in Javascript

I have the following object:

var obj = { 2017-05-28: 76.16108625212159, 2017-02-12: 70.32183347555772, 2017-05-21: 74.21070693205216, 2017-04-23: 78.49819059107358, 2017-03-05: 73.36286201022146, 2017-04-02: 79.07588060050237, 2017-01-29: 79.07021235890568, 2017-03-26: 74.79360018220122, 2017-01-22: 71.80166785183269, 2017-04-09: 72.68569443640364 };

I would like to sort it by date, beginning from January, but I have no idea how to do this.

What do you think, is it possible in JS ?

Thanks in advance,

Upvotes: 1

Views: 59

Answers (3)

Mohamed Abbas
Mohamed Abbas

Reputation: 2298

Using Array.prototype.sort() and Array.prototype.reduce()

You can not order object proprieties. so you may need to convert your object to an array

Example

var obj = {
        "2017-05-28": 76.16108625212159,
        "2017-02-12": 70.32183347555772,
        "2017-05-21": 74.21070693205216,
        "2017-04-23": 78.49819059107358,
        "2017-03-05": 73.36286201022146,
        "2017-04-02": 79.07588060050237,
        "2017-01-29": 79.07021235890568,
        "2017-03-26": 74.79360018220122,
        "2017-01-22": 71.80166785183269,
        "2017-04-09": 72.68569443640364
    },

    sortedArr = Object.keys(obj)
        .sort(function (a, b) { 
            return new Date(a) - new Date(b)
         })
        .reduce(function (acc, item, index) {
            acc.push({
                date: item,
                value: obj[item]
            });
            return acc;
        }, []);
    console.log(sortedArr);

Thanks @Shivam for your note

Upvotes: 0

Prags
Prags

Reputation: 811

var obj = '{ "2017-05-28": "76.16108625212159", "2017-02-12": "70.32183347555772", "2017-05-21": "74.21070693205216", "2017-04-23": "78.49819059107358", "2017-03-05": "73.36286201022146", "2017-04-02": "79.07588060050237", "2017-01-29": "79.07021235890568", "2017-03-26": "74.79360018220122", "2017-01-22": "71.80166785183269", "2017-04-09": "72.68569443640364" }';
pobject = JSON.parse(obj);
arr = Object.keys(pobject);
sorted = arr.sort();
len = sorted.length;
for(i=0;i<len;i++)
{
val= pobject[sorted[i]];
console.log(val);
}

This will work

Hope this helps..!

Upvotes: 1

Chirag Ravindra
Chirag Ravindra

Reputation: 4830

The order of keys is not guaranteed in an object. you can convert your data structure into a sorted array as shown below and iterate over the array in order for your needs:

var obj = {
  "2017-05-28": 76.16108625212159,
  "2017-02-12": 70.32183347555772,
  "2017-05-21": 74.21070693205216,
  "2017-04-23": 78.49819059107358,
  "2017-03-05": 73.36286201022146,
  "2017-04-02": 79.07588060050237,
  "2017-01-29": 79.07021235890568,
  "2017-03-26": 74.79360018220122,
  "2017-01-22": 71.80166785183269,
  "2017-04-09": 72.68569443640364
};

var sortedArray = Object.keys(obj).sort().map(function(key) {
  return {
    date: key,
    value: obj[key]
  }
});

console.log(sortedArray);
/**Outputs:
[
  {
    "date": "2017-01-22",
    "value": 71.80166785183269
  },
  {
    "date": "2017-01-29",
    "value": 79.07021235890568
  },
  {
    "date": "2017-02-12",
    "value": 70.32183347555772
  },
  {
    "date": "2017-03-05",
    "value": 73.36286201022146
  },
  {
    "date": "2017-03-26",
    "value": 74.79360018220122
  },
  {
    "date": "2017-04-02",
    "value": 79.07588060050237
  },
  {
    "date": "2017-04-09",
    "value": 72.68569443640364
  },
  {
    "date": "2017-04-23",
    "value": 78.49819059107358
  },
  {
    "date": "2017-05-21",
    "value": 74.21070693205216
  },
  {
    "date": "2017-05-28",
    "value": 76.16108625212159
  }
]
**/

Upvotes: 1

Related Questions