Yamaha32088
Yamaha32088

Reputation: 4163

Sorting values in object keeping indexes

I have the following object that I would like sorted:

Object {0: 1, 
        1: 2, 
        2: 1, 
        3: 2, 
        5: 2, 
        6: 1, 
        7: 2, 
        8: 1} 

This is what I would like this to become:

Object {0: 1, 
        2: 1, 
        6: 1, 
        8: 1, 
        1: 2, 
        3: 1, 
        5: 2, 
        7: 1} 

The index that contains the lowest value in it needs to be the very first instance in the new object and it should keep the same index and value. I have tried to use myObject.sort() but it does not work on objects. I have also tried iterating through it to make it into an array to be sorted but when I call sort on the resulting array it removes the index.

for(indx in mainObject){
    myNewObject[indx] = [];
    $.each(myNewObject, function(index) {
        myNewObject[indx].push(mainObject[index]);
    });
}

The above code outputs the last array as:

0: 1
1: 2
2: 1
3: 2
4: undefined
5: 2
6: 1
7: 2
8: 1

Calling the sort method on this array produces this:

[1, 1, 1, 1, 2, 2, 2, 2, undefined]

Which is excellent but now the index is not present. Can anyone tell me how to do this I have been scratching my head for hours now trying to figure it out.

Upvotes: 0

Views: 116

Answers (3)

Guffa
Guffa

Reputation: 700592

Make an array of objects, that way you can keep the index and value together:

array = [];
for(indx in mainObject){
  array.push({ index: indx, value: mainObject[indx] });
}
array.sort(function(x, y){ return x.value - y.value; });

You might want to sort on the value and then on index, as Scott Sauyet suggested. That would make sure that the result is consistent across different browsers, as they return the object properties in different order:

array.sort(function(x, y){ return x.value - y.value || x.index - y.index; });

Upvotes: 1

Scott Sauyet
Scott Sauyet

Reputation: 50807

I think you're a bit confused by your console's output. Objects are unordered collections of properties. When a console displays an object, it must show them in some order, and usually that order is likely

  • the order in which the properties were added
  • alphabetically by the property name
  • numerically by the property name, if most of the names are strings representing smallish integers

In none of these cases does any sorting of the Object properties change the underlying ideal of the object, and your console is still free to display it as desired.

So I simply don't think you can consistently achieve what you're looking for. Nor do I think it really makes fundamental sense.

If you want your output as Arrays, then we can talk...

Upvotes: 0

qw3n
qw3n

Reputation: 6334

Objects are not something you sort. You are not guaranteed any particular order when accessing properties from an object. That is why your results are converted into arrays which are sortable. Here is a possible solution

var obj={
  0: 1,
  1: 2,
  2: 1,
  3: 2,
  4: undefined,
  5: 2,
  6: 1,
  7: 2,
  8: 1
} 
var arr=[];
for (var i in obj){
  arr.push([i-0,obj[i]]);
}
arr.sort(function(a,b){
  //use whatever sorting algorithm in here.  I'm not sure exactly how you wanted it sorted

  return a[0]-b[0]||a[1]-b[1];
});

Upvotes: 0

Related Questions