1

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,

6
  • 1
    Please, copy your object and put it into the question. You can use JSON.stringify(object) to print it as string so you can copy it easily. Commented Jun 28, 2017 at 11:36
  • I've updated my post, thanks Commented Jun 28, 2017 at 11:40
  • I copied it from Firefox console] Commented Jun 28, 2017 at 11:41
  • 1
    Objects don't hold any order in Javascript, so first you need to convert it to an array. Then use array.sort() Commented Jun 28, 2017 at 11:47
  • And how to convert it to an object after the sort ? Commented Jun 28, 2017 at 11:51

3 Answers 3

1

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
  }
]
**/

Sign up to request clarification or add additional context in comments.

Comments

1
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..!

1 Comment

@john_js_py , this is done by using js native functions for sorting
0

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

2 Comments

Though this would give out the object with keys ordered, But that is not guaranteed, More about it here
@Shivam Thank you very much, Updated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.