31

I have an object like below. Trying to rearrange it in ascending order based on value. Similar to Javascript array sort method.

    var masterList = {
    "1": "google",
    "2": "yahoo",
    "3": "msn",
    "4": "stackoverflow",
    "5": "github",
    "6": "jsfiddle",
    "7": "amazon",
    "8": "ebay"
}

Please let me know the better solution...

1

3 Answers 3

63

JavaScript objects have no order. Even though most browsers do iterate in the same order the properties were created, there's no guarantee, so sorting is not supported on objects.

See here for more info: Does JavaScript Guarantee Object Property Order?

You might also be interested in what John Resig has got to say on the matter.


If you need a sort-able list, you'll have to store it as an array of objects:

var masterList = [
    { key: 1, val: "google" },
    { key: 2, val: "yahoo" },
    { key: 3, val: "msn" },
    { key: 4, val: "stackoverflow" },
    { key: 5, val: "github" },
    { key: 6, val: "jsfiddle" },
    { key: 7, val: "amazon" },
    { key: 8, val: "ebay" }
];

Then, to sort them, just use the regular array's sort method:

masterList = masterList.sort(function (a, b) {
    return a.val.localeCompare( b.val );
});

Here's the fiddle: http://jsfiddle.net/ASrUD/

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

5 Comments

Maybe I just don't understand sort(), but this doesn't work for numerical values. Trying to sort by 'key' returns undefined function.
@dval - It's the localeCompare that's throwing you off. If you're sorting by key, then just return a - b.
similar question to @dval, how would i sort the value (instead of the key) numerically? my array looks similar to this... [ {key:"Designer", val:0}, {key:"Developer", val:2}, {key:"Engineer", val:1} ]
@FinbarMaginn - list = list.sort(function(a, b){ return a.val - b.val; });
@JosephSilber this didn't work for me var arr = ["loc1", "loc2", "loc3", "loc4"]; var key = [5,3,1,2] console.log(arr.length); var obj = {}; for(i=0;i<arr.length;i++) { obj[arr[i]] = key[i]; } console.log(obj) obj = Object.entries(obj); console.log(obj) list = obj.sort(function (a, b) { return a - b; }); console.log(list) output 4 { loc1: 5, loc2: 3, loc3: 1, loc4: 2 } [ [ 'loc1', 5 ], [ 'loc2', 3 ], [ 'loc3', 1 ], [ 'loc4', 2 ] ] [ [ 'loc1', 5 ], [ 'loc2', 3 ], [ 'loc3', 1 ], [ 'loc4', 2 ] ]
9

    var obj = {
        "1": "google",
        "2": "yahoo",
        "3": "msn",
        "4": "stackoverflow",
        "5": "github",
        "6": "jsfiddle",
        "7": "amazon",
        "8": "ebay"
    };

    var arr = [];

    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            arr.push(obj[key]);
        }
    }
    
    alert(arr.sort());

This will sort your values in ascending order. let me give sometime will revert you with how to convert that to an object.

1 Comment

The only problem with this solution is that you miss the key values in the resultant array
1

var masterList = {
    "2": "yahoo",
    "3": "msn",
    "4": "stackoverflow",
    "5": "github",
    "6": "jsfiddle",
    "7": "amazon",
    "8": "ebay",
    "1": "google",
   }
var masterList_ = {}

Object.keys(masterList).sort().forEach(a=>masterList_[a]=masterList[a])
console.log(masterList_)

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.