0

how can I sort object properties by name using another array as refer?

var json = '{"b":90,"c":42, "a":34}';
var obj = JSON.parse(json);

var sorting = ["a","b","c"];

I would like to have obj properties ordered just like sorting array

Thank you

Bye

apxcode
  • 7,696
  • 7
  • 30
  • 41
pepperav
  • 527
  • 6
  • 16
  • here you can find something similar in php: http://stackoverflow.com/questions/348410/sort-an-array-based-on-another-array – rPulvi May 20 '14 at 07:59
  • The downvoted answer is the correct one, there is no guaranteed order in objects, so it can't be sorted, it's that simple. – user3649503 May 20 '14 at 07:59

6 Answers6

0
var sorting = Object.keys(obj).sort();
OrangeDog
  • 36,653
  • 12
  • 122
  • 207
0

Javascript objects are not ordered. So, you cannot actually sort them.

Jashwant
  • 28,410
  • 16
  • 70
  • 105
0

Why not just iterating over the array, and then access obj properties ?

for ( var i = 0; i < sorting.length; ++i ) {
  var current = obj[ sorting[ i ] ];
  // other stuff here...
}

If you don't intent to iterate over the obj, please explain your actual needs.

Anthony Garcia-Labiad
  • 3,531
  • 1
  • 26
  • 30
0
  1. Convert Object to Array

    var jsonArray = [];

    $.each(myObj, function(i,n) { jsonArray.push(n); });

  2. Sort Array

    var jsonArraySort = jsonArray.sort();

  3. Convert Array to Object

    var jsonSort = {}; for(var i = 0; i < jsonArraySort.length; i++) { jsonSort[i] = jsonArraySort[i]; }

Erik Lucio
  • 948
  • 7
  • 8
0

You could try something like:

var sorted = []
for (i = 0; i < sorting.length; i++) {
  if (json.hasOwnProperty(sorting[i])) {
    sorted.push(json[sorting[i]);        
  }
}

/* sorted will equal [34, 90, 42] */
izb
  • 50,101
  • 39
  • 117
  • 168
0

You cannot order the keys of an object, as per definition,

An ECMAScript object is an unordered collection of propertiesES3 Specs

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified.

Properties of the object being enumerated may be deleted during enumeration. If a property that has not yet been visited during enumeration is deleted, then it will not be visited. If new properties are added to the object being enumerated during enumeration, the newly added properties are not guaranteed to be visited in the active enumeration. A property name must not be visited more than once in any enumeration.ES5 Specs

If you want to a sorted array consisting of your objects keys, you can use [Object.keys][4], To get an array of them, which you can then sort.

var obj = {b:90,c:42, a:34}
console.log (
      Object.keys (obj).sort ()
) // ["a","b","c"]

If you are interested in a sorted array, containing both, keys and values, you could do the following.

var obj = {b: 90, c: 42, a: 34},
    srt = Object.keys(obj).sort().map(function (prop) {
              return {
                  key: prop,
                  value: obj[prop]
              }
          });

console.log(srt) //[{"key":"a","value":34},{"key":"b","value":90},{"key":"c","value":42}]
Moritz Roessler
  • 8,542
  • 26
  • 51