0

total newbie here. I need to sort array of random objects (to pass data to machine I build). How to do that?

var array = [{fgy : 34}, {sor : 56}, {dtr : 45}];

array.sort(function(a, b){return a- b;}); // ofcourse it is not working

Expected result:

fgy : 34, dtr : 45, sor : 56
john
  • 17
  • 4
  • 2
    Are you just wanting to sort on the value? Also, are you guaranteed that there will only be one key in each object? We need more info – mhodges Jun 03 '21 at 21:35
  • 2
    This solve your problem ? https://stackoverflow.com/questions/25500316/sort-a-dictionary-by-value-in-javascript – Mario Abbruscato Jun 03 '21 at 21:37
  • Does this answer your question? [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) or what @MarioAbbruscato marked would serve your purpose better. – Viv Jun 03 '21 at 21:38
  • I want the object key (name) and a value - it could be string (all key/values combined in string). I think yes, every object is just one key/value pair. – john Jun 03 '21 at 21:38

2 Answers2

3

If you are sure that there will only be one key in each object, and all you are wanting to sort by is the value, here's how you can do that:

var array = [{fgy : 34}, {sor : 56}, {dtr : 45}];

let sorted = array.slice().sort((a,b) => {
  let aVal = Object.values(a)[0];
  let bVal = Object.values(b)[0];
  return aVal - bVal;
});

console.log(sorted)

If multiple keys can be on each object, you just need to decide what you are sorting on and use that for your aVal and bVal in the .sort() function. For example, if you wanted to sort by the maximum value, you could do something like let aVal = Math.max(...Object.values(a));, similarly, you could do the sum of the values, minimum value, etc. You just need to assign a value to your aVal and bVal and then return the comparison.

ES5 syntax version (as requested in comments)

var array = [{fgy : 34}, {sor : 56}, {dtr : 45}];

let sorted = array.slice().sort((a,b) => {
  var aVal = a[Object.keys(a)[0]];
  var bVal = b[Object.keys(b)[0]];
  return aVal - bVal;
});

console.log(sorted)
mhodges
  • 10,938
  • 2
  • 28
  • 46
  • Thanks! This is working on modern computer - however on an old machine it seems I can't use "let aVal = Object.values(a)[0];". Is there an 'older' (without synthetic sugar) code I can use? It could be long, it could be ugly I just need to pass the results in proper order so I can actually do my thing, and not programing :) – john Jun 03 '21 at 22:21
0

You can use the Array.sort method, But you need to get the value inside each Object in the array. You can do it using Object.values(obj)[0]. Which will return the first value in the object

For example:

let obj = { fgy: 34 }
console.log(Object.values(obj)[0]) // returns 34

Then you can use it with the sort method:

array.sort((a, b) => {
  return Object.values(a)[0] - Object.values(b)[0]
})
  • 1
    it is an array of objects, not a simple object. – john Jun 03 '21 at 21:50
  • @john The first code snippet is demonstrating how to grab the value of an unknown key out of an object with a single key, and the second snippet uses that to grab the value from each object to use for sorting – Ken Bellows Jun 04 '21 at 11:52