Order nested objects by parameter JS

sort object of objects javascript
javascript sort array of objects
javascript sort array of objects by key value
javascript sort array of objects by multiple properties
sort object keys javascript
javascript sort array of objects not working
typescript sort array of objects
lodash sort array of objects by property

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?


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)

Quick Tip: How to Sort an Array of Objects in JavaScript, By default, the JavaScript Array.sort function converts each element in compareFunction parameter, which causes the array elements to be  You are not sending the first mandatory parameter, which actually tells the function which property you want the object sorted by: In this case, "score". So, you have to call the function like this: var ordered_scores = order_top_scores('score',returnedData);


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.

Sorting multiple object properties with JavaScript, Sorting regular arrays in javascript is a piece of cake - even sorting by object properties is simple. var array = [12, 3, 5, 22] array.sort(function(a,b){ return a - b }) To get around this we will detect if the property value is a string or a number  Higher-order functions are functions that either take functions as parameters or return functions as values (or both). They allow for a great deal of power and flexibility in programming. However, it can be difficult for beginners to understand how they are supposed to work, particularly when a nesting of higher-order functions is involved.


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);

Sorting an array of objects, With the above sort method, the employees array is now sorted by the age property, so employee[0] is "Edward", employee[1] is "George" etc. The process is very  You want to render this list, but first you want to order it by the value of one of the properties. For example you want to order it by the color name, in alphabetical order: black, red, white. You can use the sort() method of Array, which takes a callback function, which takes as parameters 2 objects contained in the array (which we call a and b):


Sorting An Object Recursively By Key In Node.JS/JavaScript, Sorting an object is one of those little things which nobody ever return key; }); // JavaScript var sortedKeys = keys.sort(function(key1, key2){ key1 = key1. Firstly, nested objects aren't touched by this code, they're copied  If you have an array of objects that you need to sort into a certain order, you might be tempted to reach for a JavaScript library. But before you do, remember that you can do some pretty neat


Destructuring assignment, We can pass parameters as an object, and the function destructuring with nested objects and colon mappings:. Objects are Passed by Reference. In JavaScript, object references are values. Because of this, objects will behave like they are passed by reference: If a function changes an object property, it changes the original value. Changes to object properties are visible (reflected) outside the function.


Iterating Over and Reducing Data, Of course, data also has the property length which would be the actual way to get the By default, the clone function will not copy over nested objects. Here is a way to sort by population in a descending order (larger populations come first). Parameters compareFunction Optional Specifies a function that defines the sort order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value. firstEl The first element for comparison. secondEl The second element for comparison. Return value. The sorted array.