Javascript d3.js:对嵌套数组排序

Javascript d3.js:对嵌套数组排序,javascript,Javascript,我怎样才能对下面的数组中的嵌套数组进行排序?我想对数组prob进行排序,然后相应地重新排列name,以便保持关系 var records = [ { num: 1, name: ["Sam", "Amy", "John"], prob: [0.3, 0.2, 0.5]}, { num: 2, name: ["Nick", "Carol", "Sam"], prob: [0.5, 0.03, 0.47] }, { num: 3, name: ["Ken", "Eric", "Ely"], prob

我怎样才能对下面的数组中的嵌套数组进行排序?我想对数组
prob
进行排序,然后相应地重新排列
name
,以便保持关系

var records = [
{ num: 1, name: ["Sam", "Amy", "John"], prob: [0.3, 0.2, 0.5]},
{ num: 2, name: ["Nick", "Carol", "Sam"], prob: [0.5, 0.03, 0.47] },
{ num: 3, name: ["Ken", "Eric", "Ely"], prob: [0.1, 0.3, 0.6] },
{ num: 4, name: ["Amy", "Sam", "John"], prob: [0.6, 0.3, 0.1] },
{ num: 5, name: ["Chris", "Donna", "Jeff"], prob: [0.25, 0.55, 0.2] }
]
最后,我想说:

var records = [
{ num: 1, name: ["John", "Sam", "Amy"], prob: [0.5, 0.3, 0.2]},
{ num: 2, name: ["Nick", "Sam", "Carol"], prob: [0.5, 0.47, 0.03] },
{ num: 3, name: ["Ely", "Eric", "Ken"], prob: [0.6, 0.3, 0.1] },
{ num: 4, name: ["Amy", "Sam", "John"], prob: [0.6, 0.3, 0.1] },
{ num: 5, name: ["Donna", "Chris", "Jeff"], prob: [0.55, 0.25, 0.2] }
]

谢谢

如果您将
name
s和
prob
s的数组转换为对象,以保留每个名称和概率之间的链接,这是最简单的。然后,可以使用自定义排序函数对对象的属性进行排序,就像它们是一个数组一样

records.forEach( function (d) {
  var temp = {};

  // create objects with key <name> and value <prob>
  // (this makes the assumption that prob values may not be unique, but names are)
  d.name.forEach( function(e,i) {
    temp[e] = d.prob[i];
  })

  // get an array of the object keys (the name), and sort them by the object values
  // (the corresponding prob), descending. Replace the existing `name` array with this.
  d.name = Object.keys(temp).sort(function(a, b){ 
    return temp[b] - temp[a];
  });

  // sort the prob values in descending order (with d3.descending, since you mentioned d3)
  d.prob = d.prob.sort(function(a,b){ return b - a; });
});
records.forEach(函数(d){
var temp={};
//使用键和值创建对象
//(这使得假设prob值可能不是唯一的,但名称是唯一的)
d、 名称.forEach(函数(e,i){
温度[e]=d.prob[i];
})
//获取一个对象键数组(名称),并按对象值对它们进行排序
//(对应的prob),降序。将现有的'name'数组替换为此数组。
d、 name=Object.keys(temp).sort(函数(a,b){
返回温度[b]-温度[a];
});
//按降序对prob值进行排序(使用d3.descending,因为您提到了d3)
d、 prob=d.prob.sort(函数(a,b){返回b-a;});
});

如果您不使用d3,请使用标准的降序排序函数替换
d3.descending

这里有一个解决方案:我们可以将
名称
prob
成对存储,同时对这两个值进行排序,然后将排序后的名称和prob分配给主对象:

records.forEach( function (d) {
  var temp = {};

  // create objects with key <name> and value <prob>
  // (this makes the assumption that prob values may not be unique, but names are)
  d.name.forEach( function(e,i) {
    temp[e] = d.prob[i];
  })

  // get an array of the object keys (the name), and sort them by the object values
  // (the corresponding prob), descending. Replace the existing `name` array with this.
  d.name = Object.keys(temp).sort(function(a, b){ 
    return temp[b] - temp[a];
  });

  // sort the prob values in descending order (with d3.descending, since you mentioned d3)
  d.prob = d.prob.sort(function(a,b){ return b - a; });
});
var记录=[
{num:1,name:[“萨姆”、“艾米”、“约翰”],prob:[0.3,0.2,0.5]},
{num:2,name:[“尼克”、“卡罗尔”、“萨姆”],prob:[0.5,0.03,0.47]},
{num:3,name:[“肯”、“埃里克”、“伊利”],prob:[0.1,0.3,0.6]},
{num:4,name:[“艾米”、“萨姆”、“约翰”],prob:[0.6,0.3,0.1]},
{num:5,name:[“克里斯”、“唐娜”、“杰夫”],prob:[0.25,0.55,0.2]}
];
变量对=记录.map((e,i)=>
e、 name.map((e2,i2)=>e2,记录[i].prob[i2]]).sort((a,b)=>b[1]-a[1])
);
记录.map((e,i)=>{
e、 name=pairs[i].map(o=>o[0])
e、 prob=pairs[i].map(o=>o[1])
})

console.log(记录)
重构

var记录=[
{num:1,name:[“萨姆”、“艾米”、“约翰”],prob:[0.3,0.2,0.5]},
{num:2,name:[“尼克”、“卡罗尔”、“萨姆”],prob:[0.5,0.03,0.47]},
{num:3,name:[“肯”、“埃里克”、“伊利”],prob:[0.1,0.3,0.6]},
{num:4,name:[“艾米”、“萨姆”、“约翰”],prob:[0.6,0.3,0.1]},
{num:5,name:[“克里斯”、“唐娜”、“杰夫”],prob:[0.25,0.55,0.2]}
];
记录。forEach((e,i)=>{
var el=e.name.map((e2,i2)=>[e2,e.prob[i2]]);
el.sort((a,b)=>b[1]-a[1]);
e、 name=el.map(o=>o[0]);
e、 prob=el.map(o=>o[1]);
});

控制台日志(记录)
name
prop
的元素组合在具有
name
prop
属性的对象数组中。按
prop
对该数组进行排序,从已排序的objectshow数组中创建
name
prop
数组显示此d3.js是否相关?您能否澄清“相应的名称以便保持关系。”。你如何对名称数组进行排序(按什么顺序)。对象数组,压缩数组
name
prob
你在研究中发现了什么?使用两种不同的排序方法,两种硬编码方法都在等待一个很难跟踪的错误,如果代码被修改。使用相同的排序函数。d3.descending执行与标准数字排序相同的排序函数。非常感谢各位,这就是我最终得到的结果,与rioV8和Emmeus提出的结果非常接近:var out=[];对于(var i=0;i需要保留
对的中间结果是什么?@rioV8同时对两个值进行排序否。只需排序
el
。缩进有助于可读性。
也有帮助,使用
.map()构造
el
el
是一个可以排序的数组,==
e
可以成对使用。forEach
另一个优点@rioV8,谢谢。我太专注于主要逻辑了