5

I have a javascript object with two array's as shown,

var Object = {'name': [Matt, Tom, Mike...], 'rank': [34,1,17...]};

I am trying to sort by rank 1,2,3.. but keep the name associated with the rank.

Object.name[0] // tom
Object.rank[0] // tom's rank of 1.

Should I reconfigure my object to make sorting easier?

I am currently using the

 Object.rank.sort(function(a,b){return a-b});

to order rank, but the name does not stay with it.

All help appreciated. Thanks!

2
  • 1
    Better to change your object to hold data in a different format and Object is a bad variable name. Commented Apr 9, 2012 at 23:43
  • My object is not named object.. I just thought it would be simpler... guess not.. Commented Apr 9, 2012 at 23:50

4 Answers 4

4

Yes, reconfigure. Say you had this instead:

var people = [{name:"Matt", rank:34}, {name:"Tom", rank:1}, {name:"Mike", rank:17}];

Then you could sort like this:

people.sort(function(a, b) {
  return a.rank - b.rank;
}

Edit

Since you have parallel lists, just zip them together:

var people = [];
for (var i = 0; i < Object.name.length; i++) {
  people.push({name:Object.name[i], rank:Object.rank[i]});
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks! I knew there had to be a more intuitive way around this.
so. I have my people object as you show. [{name:val}, {name:val}], But I am struggling to alert(people.name[0]) and also my var, while created with out of the function, and with a async:false POST, can not be used outside code... Any ideas?
That would actually be people[0].name.
2

The real world object:

 o = {name: ['Matt', 'Tom', 'Mike'], rank: [34,1,17]};

Make an array for better data structure:

var arr =[]; 
o.name.forEach(function(name, i){
      arr.push({name: name, rank: o.rank[i]})
});

Sort by rank:

arr.sort(function(a,b){return a.rank - b.rank});

Sort by name:

arr.sort(function(a,b){return a.name- b.name});

Revert back to your original data structure:

o = {name:[], rank:[]}
arr.forEach(function(item){
   o.name.push(item.name);
   o.rank.push(item.rank);
});

3 Comments

forEach is nice. Is that a Mozilla extension or is it cross-browser?
ForEach is available in most browsers and can have a fallback in older browsers.
Thanks Mohsen! I like your forEach method best for reconfiguring the current object
1

Well, yes, if the i-th object in names array is connected to the i-th object in the rank array, you should represent it that way. This means, you should use a Person (or whatever it is) object with two properties: name and rank.

// person constructor
function Person(name, rank) {
    this.name = name;
    this.rank = rank;
}

// create the object with the array
var myObject = {
    myArray: new Array()
};

// populate the array
myObject.myArray.push(new Person('Matt', 34));
myObject.myArray.push(new Person('Tom', 1));
myObject.myArray.push(new Person('Mike', 17));

// sort the Person objects according to their ranks
myObject.myArray.sort(function(a, b) {
    return b.rank - a.rank;    
});

3 Comments

Great! looks solid. Ill give it a go!
In this case not sure making a special Person object has any advantage over a generic object. I guess later down the line you might want to do instanceof on it?
@darkporter Well that's hard to tell from one snippet of code, isn't it? Maybe a special Person object will come in handy for further functionality, maybe not at all...
0

You'll have to write your own sort function that for each sorting operation, remembers what index comes where per iteration in the ranks array. The do the same move from source index to destination index in the names array. (edit) One algortihm for this from the top of my head is the bubblesort, look it up.

The other option is to look for some kind of "map" collection implementation.

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.