0

Currently I have a small piece of code which loops through a json object:

for (var key in json_object) {
    if (json_object.hasOwnProperty(key)) {
        var value       = key; //e.g. 50_100, 1000_2000, 20_50 etc
    }
}

I'm going to be outputting these values into a list later on. But the problem is that these values aren't in any order right now.

I'd like to be able to have these values sorted out in order. So my question is, is this possible and if so how?

Thanks!

6
  • 1
    Object keys don't have an order. If defined at compile time, order of keys is retained, else sorted by alphabetical order of keyname Commented Mar 16, 2016 at 13:48
  • 1
    You can make array of these key values and than sort it. As #gurvinder372 said - no sort for keys in object Commented Mar 16, 2016 at 13:50
  • My purpose is to be able to output them in an ordered list later on, so is there any alternative or other ways to achieve what I need? Commented Mar 16, 2016 at 13:53
  • @S.Nadezhnyy do you mean I save these values into an array and then sort that array? Commented Mar 16, 2016 at 13:53
  • @user2028856: You could serve an array (list) in the JSON that has the right order up front. Commented Mar 16, 2016 at 13:54

3 Answers 3

1

In javascript, object properties are not guaranteed a specific order, so if you want to maintain order, you would likely need to write the object properties into an array of objects.

That could look like this:

var object_array = [];
// map properties into array of objects
for (var key in json_object) {
    if (json_object.hasOwnProperty(key)) {
        object_array.push({
            'key': key;
            'value': value;
        });   
    }
}
// sort array of objects
object_array.sort(function(a,b) {
    // note that you might need to change the sort comparison function to meet your needs
    return (a.value > b.value);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this but it doesn't seem to perform any sorting for strings such as this "100_500" , "800_1000", "2000_3000" etc
@user2028856 That is why I indicated that you might need to change the sort comparison to do something applicable to the values you are sorting by. In the case of your strings, how would you propose to perform the sort?
1

After reading all solutions, I realized my solution was wrong. Object.keys is good for getting an array of keys only, so Mike Brant's solution is correct, although a little fix is needed since value is not a variable there. Finally here's a fixed solution based on his solution + the requested sorting from the comments:

var arr = [];

for (var key in json_object) {
    if (json_object.hasOwnProperty(key)) {
        arr.push({
            'key': key;
            'value': json_object[key];
        });   
    }
}

// this will sort by the first part of the string
arr.sort(function(a, b) {
  return a.key.split('_')[0] - b.key.split('_')[0];  
}

Hope this helps... :-)

10 Comments

What is value? Probably not an array index.
Can you please tell me what should I use for 'value'?
You can directly use Object.keys(obj).sort(function(){})
@user2028856 Fixed and also added another option following Rajesh's comment
Does this method provide sorting for strings like this: "100_500" , "800_1000", "2000_3000" etc?
|
0

The following fixes eslint issues I found in the other solutions:

var arr = [];
Object.keys(json_object).forEach(function(key) {
  arr.push({
    'key': key,
    'value': json_object[key]
  });  
})

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.