1

I have a JS Object which contains a list of chats. What i need is to sort them in desc order according to the last_message_at value which is an epoch (Unix).

The structre of the Object is as below

var test = {
    "138545616542461": {
        "otherUser": {
            "$id": "128636544201178",
            "$priority": null,
            "age": 35
        },
        "last_message_at": 1268482500
    },
    "138545616542460": {
        "otherUser": {
            "$id": "128636544201178",
            "$priority": null,
            "age": 35
        },
        "last_message_at": 1368482500
    }
};

Typically, the sorted object should have 1368482500 at the Top. Array can be used for sorting purposes but finally the result should be an Object.

Tried some stackoverflow examples with no luck..

2
  • 4
    You are using the wrong type of data structure if you're expecting to sort the keys of an object on the object. Use an array for sorted order, not a plain object. Commented Jul 15, 2016 at 5:52
  • It is not a wrong data type if Object is wanted instead of Array! The program get VERY much faster to find elements with test["138545616542460"] instead of some test.find("138545616542460") looping an array. Complexity come down from O(n) to O(1) !!! Commented May 18, 2019 at 19:46

2 Answers 2

1

There are a number of important issues prohibiting the exact steps you are trying to accomplish so I've taken the liberty of a quick method that does what I think you want -- namely enumerating the keys of the object and sorting by the numeric value of last_message_at

Object.keys(test)
      .sort((a,b) => {
             return test[a].otherUser.last_message_at - test[b].otherUser.last_message_at; })
      .map((elt) => { return test[elt]; });

This function returns an array (because it must). As @jfriend00 pointed out object property order is never guaranteed in Javascript.

Another point to be made is that you cannot have an object with duplicate keys -- your test object supplied will only have one key when the initialization of the object is finished.

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

3 Comments

But i'm losing key while doing so
Yes, that's exactly right -- you can't have an object with duplicate keys.
But the keys are different.. see the last 1 and 0 end of the key
0

The order of properties in an object is not guaranteed in Javascript.

Definition of an Object from ECMAScript:

4.3.3 Object

An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

You should either use arrays or map objects which guarantees key order.

Comments

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.