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"}]
6
  • 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]); }); Commented Jul 8, 2014 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. Commented Jul 8, 2014 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 Commented Jul 8, 2014 at 10:24
  • 1
    Array.prototype.sort accepts a sort function as a parameter. Have a play with that. Commented Jul 8, 2014 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. Commented Jul 8, 2014 at 10:27

4 Answers 4

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.

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

Comments

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

Comments

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.

Comments

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;
}

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.