25

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...

r1webs
  • 303
  • 1
  • 3
  • 9
  • This post would help you how to do it, http://stackoverflow.com/questions/881510/json-sorting-in-jquery – Adil Jan 08 '13 at 05:24

3 Answers3

56

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/

Community
  • 1
  • 1
Joseph Silber
  • 193,614
  • 53
  • 339
  • 276
  • Maybe I just don't understand sort(), but this doesn't work for numerical values. Trying to sort by 'key' returns undefined function. – dval Nov 11 '14 at 14:17
  • 3
    @dval - It's the `localeCompare` that's throwing you off. If you're sorting by `key`, then just `return a - b`. – Joseph Silber Nov 11 '14 at 14:25
  • @Joseph Silber Ah! Perfect. Thanks. – dval Nov 11 '14 at 14:43
  • 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} ]` – Finbar Maginn Aug 03 '15 at 12:59
  • 3
    @FinbarMaginn - `list = list.sort(function(a, b){ return a.val - b.val; });` – Joseph Silber Aug 03 '15 at 16:51
  • @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 – SudhirKumar Aug 10 '19 at 21:56
8

    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.

therj
  • 116
  • 1
  • 9
  • 1
    The only problem with this solution is that you miss the key values in the resultant array – Alex Aug 11 '17 at 08:28
0

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_)