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
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 Commented Jun 3, 2021 at 21:35
  • 2
    This solve your problem ? stackoverflow.com/questions/25500316/… Commented Jun 3, 2021 at 21:37
  • Does this answer your question? Sort array of objects by string property value or what @MarioAbbruscato marked would serve your purpose better. Commented Jun 3, 2021 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. Commented Jun 3, 2021 at 21:38

2 Answers 2

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)

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

1 Comment

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 :)
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]
})

2 Comments

it is an array of objects, not a simple object.
@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

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.