1

I have an object in JS structured as follows:

[{
 "x": 2.31,
 "y": 0.538
}, 
{
 "x": 7.07,
 "y": 0.469
}, 
{
 "x": 6.02,
 "y": 0.469
}, 
{
 "x": 2.18,
 "y": 0.458
}]

I need to sort by one of the keys in each element (sort by x). The result would look like this:

[{
 "x": 2.18,
 "y": 0.458
}, 
{
 "x": 2.31,
 "y": 0.538
}, 
{
 "x": 6.02,
 "y": 0.469
}, 
{
 "x": 7.07,
 "y": 0.469
}]

The following approach doesn't work with the above structure:

var sorted = [];
for(var key in dict) {
 sorted[sorted.length] = key;
}
sorted.sort();

Nor does the following:

function sortObject(o) {
 return Object.keys(o).sort().reduce((r, k) => (r[k] = o[k], r), {});
}
Nimeshka Srimal
9,1605 gold badges48 silver badges62 bronze badges
asked Jan 22, 2018 at 5:34
3

5 Answers 5

1

Implement a custom sort method.

var arr = [{ "x": 2.31, "y": 0.538 }, { "x": 7.07, "y": 0.469 }, { "x": 6.02, "y": 0.469 }, { "x": 2.18, "y": 0.458 }]
arr.sort((a,b) => a.x - b.x)
console.log(arr);

answered Jan 22, 2018 at 5:49
Sign up to request clarification or add additional context in comments.

Comments

0

Yes, you can sort the key value of the object by comparing the key's value using parseFloat.

var coordinates=[{
 "x": 2.31,
 "y": 0.538
}, 
{
 "x": 7.07,
 "y": 0.469
}, 
{
 "x": 6.02,
 "y": 0.469
}, 
{
 "x": 2.18,
 "y": 0.458
}]
var sortedKey=coordinates.sort(function(a, b) {
 return parseFloat(a.x) - parseFloat(b.x);
});
console.log(sortedKey);

answered Jan 22, 2018 at 5:47

1 Comment

The values are already numbers sow by should it be relevant for this example to use parseFloat?
0

You can use lodash. https://lodash.com/docs/4.17.4#sortBy

lodash is a modern JavaScript utility library delivering modularity, performance & extras.

Demo: http://jsfiddle.net/e2ccmbhh

let arr = [{
 "x": 2.31,
 "y": 0.538
}, 
{
 "x": 7.07,
 "y": 0.469
}, 
{
 "x": 6.02,
 "y": 0.469
}, 
{
 "x": 2.18,
 "y": 0.458
}]
let sortedArr = _.sortBy(arr, 'x');
answered Jan 22, 2018 at 5:48

Comments

0

You can use Javascript builtin sort() method to do that.

var myList = [{
 "x": 2.31,
 "y": 0.538
 },
 {
 "x": 7.07,
 "y": 0.469
 },
 {
 "x": 6.02,
 "y": 0.469
 },
 {
 "x": 2.18,
 "y": 0.458
 }];
 var sortedList = myList.sort(function(a, b){
 return a.x - b.x;
 });
 console.log(sortedList);
answered Jan 22, 2018 at 5:51

Comments

0

What you have do here is implement a custom sort method for comparison between the elements in your array.

Try this out.

const array = [{
 "x": 2.31,
 "y": 0.538
}, 
{
 "x": 7.07,
 "y": 0.469
}, 
{
 "x": 6.02,
 "y": 0.469
}, 
{
 "x": 2.18,
 "y": 0.458
}];
array.sort((obj1, obj2) => {
 return obj1.x - obj2.x;
});
answered Jan 22, 2018 at 5:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.