admin管理员组文章数量:1296339
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!
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!
Share Improve this question edited May 1, 2012 at 0:22 Joe Doyle 6,3833 gold badges44 silver badges45 bronze badges asked Apr 9, 2012 at 23:40 mattydmattyd 1,6832 gold badges17 silver badges26 bronze badges 2- 1 Better to change your object to hold data in a different format and Object is a bad variable name. – epascarello Commented Apr 9, 2012 at 23:43
- My object is not named object.. I just thought it would be simpler... guess not.. – mattyd Commented Apr 9, 2012 at 23:50
4 Answers
Reset to default 4Yes, 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]});
}
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);
});
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;
});
You'll have to write your own sort function that for each sorting operation, remembers what index es 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.
本文标签: sortingsort javascript array in objectmaintaining keyStack Overflow
版权声明:本文标题:sorting - sort javascript array in object, maintaining key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741621464a2388835.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论