1

Is there a way to sort nested objects by one of their parameters?

For example, if I have a data structure like this:

var someObject = {
  'part1328': {
    'time': 1543203609575,
  },
  'part38321': {
    'time': 1543203738716,
  },
  'part1328': {
    'time': 1543203746046,
  },
  'part38338': {
    'time': 1543203752264,
  }

};

and I don't know how many parts I'll have in advance or what their names will be. Is there a way I can sort the parts by their time and get the most recent and oldest parts?

7
  • 1
    Could you add what the output should be? Commented Nov 26, 2018 at 3:37
  • 1
    You should not assume that an object has some kind of ordering. So what do you mean by the "first" and "last" part? Are they determined by the number in their key? Commented Nov 26, 2018 at 3:38
  • 2
    IMK Property order is not mantianed in Javascript objects so first/last can differ Commented Nov 26, 2018 at 3:38
  • 1
    Objects do not keep insertion order state; Maps do, however. You will have to elaborate on expected output and how this may be sorted given an object. Commented Nov 26, 2018 at 3:41
  • Other things aside, you will be doing your future self a favor if you are consistent about the values your object. Right now some are objects and one is an array. It will make this an annoying data structure. Commented Nov 26, 2018 at 3:43

3 Answers 3

3

You can use Object.entries to get the set of key/value pairs as a list. Then you can sort that list and arrange the data however you like:

var someObject = {
  'part1328': {
    'time': 1543203609575,
  },
  'part38321': {
    'time': 1543203738716,
  },
  'part1328': {
    'time': 1543203746046,
  },
  'part38338': {
    'time': 1543203752264,
  }

};

let arr = Object.entries(someObject).sort((a, b) => a.time - b.time)
console.log(arr)


// from here you can manage the data any way you want. 
// for example, an array of simple objects:

let merged = arr.map(([key, value]) => ({id: key, ...value}) )
console.log(merged)

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

2 Comments

Is the benefit of using Object.entries that I get the full object, not just the key name sorted (this is in reference to Amadan's solution)?
@TransmissionsDev yes Object.entries is a little more convenient as it gets you a list [key, value] pairs. You can also use Object.keys and then lookup the value. One import difference is that Object.keys is supported by IE and entries() isn't.
1

You cannot sort an object. You can sort a list of object's keys, you can sort object's values, or the list of pairs of a key and a corresponding value ("entries"). Here's the first approach:

Object.keys(someObject).sort((a, b) => a.time - b.time)
// => ["part1328", "part38321", "part38338"]

You can then use these sorted keys to access the values in the original object in the desired order.

Note also that objects can't have repeating keys; they just overwrite each other. Thus, the fourth value is gone even before you assigned it to someObject.

2 Comments

Would I then just use bracket notation to retrieve the actual part?
Yeah, so someObject[sortedKeys[0]] would be the earliest record.
0

You can maintain the sorted list by creating an array sorted by time

Code:

const someObject = {
  'part1328': {
    'time': 1543203609575,
  },
  'part38321': {
    'time': 1543203738716,
  },
  'part1328': {
    'time': 1543203746046,
  },
  'part38338': {
    'time': 1543203752264,
  }
};

const result = Object.keys(someObject)
  .sort((a, b) => someObject[a].time - someObject[b].time)
  .map(k => ({ [k]: someObject[k] }));

console.log(result);

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.