简体   繁体   English

在 object 中对 javascript 数组进行排序,维护密钥

[英]sort javascript array in object, maintaining key

I have a javascript object with two array's as shown,我有一个 javascript object 有两个数组,如图所示,

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.我正在尝试按等级 1、2、3 排序。但保持名称与等级相关联。

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

Should I reconfigure my object to make sorting easier?我应该重新配置我的 object 以使排序更容易吗?

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!谢谢!

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:由于您有并行列表,因此只需 zip 将它们放在一起:

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:现实世界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.嗯,是的,如果names数组中的第 i 个 object 连接到rank数组中的第 i 个 object,你应该这样表示。 This means, you should use a Person (or whatever it is) object with two properties: name and rank .这意味着,您应该使用具有两个属性的Person (或任何它) object : namerank

// 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 comes where per iteration in the ranks array.您必须为每个排序操作编写自己的排序 function,记住在 ranks 数组中每次迭代出现的索引。 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.另一种选择是寻找某种“地图”集合实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM