1

I am trying all that I know in javascript. Is there a way to sort this based on key?

var myObj =[{"2":"installation.diagnostics.status"},{"4":"installation.diagnostics.status"},{"3":"installation.diagnostics.status"}];

Here is my code!!

var result=JSON.stringify(navMenu);

eval('var obj='+result);

console.log(obj);
Object.keys(obj)
      .sort()
      .forEach(function (k) {
         console.log(obj[k]);
      });

to

var myObj =[{"2":"installation.diagnostics.status"},{"3":"installation.diagnostics.status"},{"4":"installation.diagnostics.status"}]
  • I have tried, and why negative marking!! var result=JSON.stringify(navMenu); eval('var obj='+result); console.log(obj); Object.keys(obj) .sort() .forEach(function (k) { console.log(obj[k]); }); – user3711239 Jul 08 '14 at 10:22
  • 1
    Then edit you answer to add what you tried, and tell us what you get and what you expected. We aren't here to write your code. – OlivierH Jul 08 '14 at 10:23
  • Fine, I have edited my code, I am not able to get in to the keys and sort it. Its sorting on array keys like 0,1 and 2 not on 2,4,3 – user3711239 Jul 08 '14 at 10:24
  • 1
    [*Array.prototype.sort*](http://ecma-international.org/ecma-262/5.1/#sec-15.4.4.11) accepts a sort function as a parameter. Have a [play with that](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). – RobG Jul 08 '14 at 10:24
  • Do the objects always have only one property that's numeric? Otherwise, the question is unclear as to how exactly the sort should take place. – Ja͢ck Jul 08 '14 at 10:27
  • Yes, one numeric and string. The number can be between 1 to 100. just like this. [{"2":"installation.diagnostics.status"},{"4":"installation.diagnostics.status"},{"3":"installation.diagnostics.status"}] – user3711239 Jul 08 '14 at 10:30

4 Answers4

2

Given:

var myObj =[{"2":"installation.diagnostics.status"},{"4":"installation.diagnostics.status"},{"3":"installation.diagnostics.status"}];

you can sort that using:

myObj.sort(function(a, b){return Object.keys(a)[0] - Object.keys(b)[0]});

You may need a polyfill for Object.keys to support older browsers.

Note that this sorts on the first returned property name as a number. If any member of myObj has more than one own property or the first returned isn't a digit, it may not work as expected.

RobG
  • 142,382
  • 31
  • 172
  • 209
1

You need to use a comparator function. This is the one to sort by strings which is what the object keys are.

function compare(a, b) {
  var ak = Object.keys(a)[0];
  var bk = Object.keys(b)[0];
  if (ak < bk) return -1;
  if (ak > bk) return 1;
  return 0;
}

myObj.sort(compare);

DEMO

Andy
  • 61,948
  • 13
  • 68
  • 95
0

Try this :

 (function (s) {
    var t = {};
    Object.keys(s).sort().forEach(function (k) {
        t[k] = s[k]
    });
    return t
})(myObj)

It works for me.

iJade
  • 23,144
  • 56
  • 154
  • 243
robieee
  • 364
  • 2
  • 18
0
function sortArrayOfObjectsByKey(array, key, order){
    // can sort forward or in reverse - can be passed as integer or string
    // if order not passed in, defaults to 1 (forward)
    order = !order ? 1 : order;

    if (order === -1 || order.toString().toLowerCase() === "reverse"){
        array.sort(function(first, second) {
            if (first[key] < second[key]) { return 1; }
            else if (first[key] > second[key]) { return -1; }
            return 0;
        });
    }
    else {
        array.sort(function(first, second) {
            if (first[key] < second[key]) { return -1; }
            else if (first[key] > second[key]) { return 1; }
            return 0;
        });
    }
    return array;
}
cortexlock
  • 1,446
  • 2
  • 13
  • 26