1

Thanks ahead for your help!

I'm trying to reorder some objects so the newest ordered product would be first. So looking at the data below, the new order should be (from newest to oldest) product2, product3, and then product1.

{
    "candyResponse": {
        "product1": {
            "displayName": "Bubble Gum",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod1?$146$",
            "orderDate": {
                "time": "11/03/2018"
            }
        },
        "product2": {
            "displayName": "Chocolate",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod2?$146$",
            "orderDate": {
                "time": "03/05/2015"
            }
        },
        "product3": {
            "displayName": "Mints",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod3?$146$",
            "orderDate": {
                "time": "09/20/2017"
            }
        }
    }
}

I tweaked the code from Sorting an array of JavaScript objects but I was not successful. Tried three ways...

candyResponse.sort(function(a, b) {
   return parseFloat(a.time) - parseFloat(b.time);
});

candyResponse.sort(function(a, b) {
   return parseFloat(a.orderDate) - parseFloat(b.orderDate);
});

candyResponse.sort(function(a, b) {
   return parseFloat(a.orderDate.time) - parseFloat(b.orderDate.time);
});

Thanks again for your help!

2
  • I would suggest you convert the JSON to an object first, because JSON is just a string and you wont have the properties you want Commented Apr 19, 2018 at 0:46
  • No, no, no. Object's keys are unable to be sorted. Commented Apr 19, 2018 at 0:48

2 Answers 2

0

candyResponse is an object, not an array - objects' property names are not reliably ordered, per the spec, so even if you did create a new object with the properties in the desired insertion order, it wouldn't be something to rely on.

Sort an array instead:

const candyResponse = {
  "product1": {
    "displayName": "Bubble Gum",
    "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod1?$146$",
    "orderDate": {
      "time": "11/03/2018"
    }
  },
  "product2": {
    "displayName": "Chocolate",
    "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod2?$146$",
    "orderDate": {
      "time": "03/05/2015"
    }
  },
  "product3": {
    "displayName": "Mints",
    "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod3?$146$",
    "orderDate": {
      "time": "09/20/2017"
    }
  }
};
const getTime = ([_, product]) => {
  return new Date(product.orderDate.time);
};
const sortedCandyArr = Object.entries(candyResponse)
  .sort((a, b) => getTime(b) - getTime(a));
console.log(sortedCandyArr);

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

1 Comment

CertainPerformance, thanks this is really helpful! And thanks for clarifying candyResponse.
-1

Well, you don't have an array of javascript objects, you just have a javascript object. That being said, you could do:

var data = {
    "candyResponse": {
        "product1": {
            "displayName": "Bubble Gum",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod1?$146$",
            "orderDate": {
                "time": "11/03/2018"
            }
        },
        "product2": {
            "displayName": "Chocolate",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod2?$146$",
            "orderDate": {
                "time": "03/05/2015"
            }
        },
        "product3": {
            "displayName": "Mints",
            "imageURL": "https://dummyimage.com/200x200/000/ffffff.png&text=prod3?$146$",
            "orderDate": {
                "time": "09/20/2017"
            }
        }
    }
}

var sorted = Object.keys(data.candyResponse)
            .sort((a, b) => 
                 new Date(data.candyResponse[a].orderDate.time).getTime() - 
                  new Date(data.candyResponse[b].orderDate.time).getTime());
                  
data.candyResponse = sorted.reduce((carry, current) => { carry[current] = data.candyResponse[current]; return carry; }, {});
console.log(data)

7 Comments

This is not reliable and mustn't be considered to "solve" the problem.
not reliable... how? he has an object. the only way to even think of it as "sorted" would be this way. If he does want an array, it's pretty clear how to do it - you use .map instead of .reduce. But there isn't a "more reliable" way to "sort" an object.
Just take a look: Does JavaScript Guarantee Object Property Order? your approach is not reliable for sure.
@Ele - That reference (from 7 yrs ago) is no longer valid. ES6 does provide certainty of object property order based upon, int prop types ,followed by strings, followed by Symbols. That references also relies on the third edition of the spec. Here is the active (if you can call it that) definition: ecma-international.org/ecma-262/6.0/… Please note the lack of mention of order in that definition compared to the earlier version.
@RandyCasburn the order of property names is still not guaranteed nowadays.
|

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.