[英]Javascript: Sort multi dimensional Array based on first arrays sort results
I have an array setup similar to this: 我有一个与此类似的数组设置:
var ary1 = new Array("d", "a", "b", "c");
var ary2 = new Array("ee", "rr", "yy", "mm");
var mdAry = new Array(ary1, ary2);
ary1 and ary2 indexes are info related to each other in the grand scheme of things. ary1和ary2索引是事物的整体方案中彼此相关的信息。
d ee
a rr
b yy
c mm
I can sort() ary1 and get: 我可以sort()ary1并获取:
a
b
c
d
but if I sorted ary2 independently I would get: 但是,如果我对ary2独立排序,则会得到:
ee
mm
rr
yy
which visually breaks ary1 and ary2 connections when listed out. 列出后,从视觉上中断了ary1和ary2连接。 Can I retrieve ary1's sorted solution and apply that to ary2?
我可以检索ary1的排序解决方案并将其应用于ary2吗? I want to get this:
我想得到这个:
a rr
b yy
c mm
d ee
If not, could mdAry be sorted so that it applies mdAry[0] sorted solution to the remaining indicies? 如果不是,是否可以对mdAry进行排序,以便将mdAry [0]排序的解决方案应用于其余指标?
If your array items are related, then store them together: 如果您的数组项相关,则将它们存储在一起:
var arr = [
{x: 'd', y: 'ee'},
{x: 'a', y: 'rr'},
{x: 'b', y: 'yy'},
{x: 'c', y: 'mm'}
];
arr.sort(function(a, b) {
if (a.x != b.x) {
return a.x < b.x ? -1 : 1;
}
return 0;
});
One way to do this is to transform the data structure to something that can be sorted more easily, and then transform it back after 实现此目的的一种方法是将数据结构转换为可以更轻松地排序的内容,然后在之后将其转换回
var ary1 = ["d", "a", "b", "c"],
ary2 = ["ee", "rr", "mm", "yy"]
mdAry = [ary1, ary2];
// convert to form [[d, ee], [a, rr], ..]
var tmp = mdAry[0].map(function (e, i) {
return [e, mdAry[1][i]];
});
// sort this
tmp.sort(function (a, b) {return a[0] > b[0];});
// revert to [[a, b, ..], [rr, mm, ..]]
tmp.forEach(function (e, i) {
mdAry[0][i] = e[0];
mdAry[1][i] = e[1];
});
// output
mdAry;
// [["a", "b", "c", "d"], ["rr", "mm", "yy", "ee"]]
Just to add yet another method in there, you could get a sort "result" from the first array and apply that to any other related list: 只需在其中添加另一种方法,您就可以从第一个数组中获得一种“结果”并将其应用于任何其他相关列表:
function getSorter(model) {
var clone = model.slice(0).sort();
var sortResult = model.map(function(item) { return clone.indexOf(item); });
return function(anyOtherArray) {
result = [];
sortResult.forEach(function(idx, i) {
result[idx] = anyOtherArray[i];
});
return result;
}
}
Then, 然后,
var arr = ["d", "a", "b", "c"];
var arr2 = ["ee", "rr", "yy", "mm"];
var preparedSorter = getSorter(arr);
preparedSorter(arr2);
//=> ["rr", "yy", "mm", "ee"];
Or, 要么,
multidimensional = [arr, arr2];
multidimensional.map(getSorter(arr));
// => [["a", "b", "c", "d"], ["rr", "yy", "mm", "ee"]]
In your example the result should be (if I understand correctly) 在您的示例中,结果应该是(如果我理解正确的话)
a rr
b mm
c yy
d ee
So this one should to the job: 因此,这一工作应该:
Array.prototype.sortRelated = function(related) {
var clone = this.slice(0),
sortedRelated = [];
clone.sort();
for(var i = 0; i < this.length; i ++) {
sortedRelated[clone.indexOf(this[i])] = related[i];
}
return sortedRelated;
}
var ary1 = new Array("d", "a", "b", "c");
var ary2 = new Array("ee", "rr", "mm", "yy");
var sorted = ary1.sortRelated(ary2);
Working demo here: http://jsfiddle.net/cwgN8/ 此处的演示工作: http : //jsfiddle.net/cwgN8/
You could "merge" them into a single object with two properties, sort by the first one, and then separate back in the end ( see demo here ): 您可以将它们“合并”为具有两个属性的单个对象,按第一个属性排序,然后最后分开( 请参见此处的演示 ):
function sortBoth(ary1, ary2) {
var merged = [];
for (var i=0; i < ary1.length; i++) merged.push({'ary1': ary1[i], 'ary2': ary2[i]});
merged.sort(function(o1, o2) { return ((o1.ary1 < o2.ary1) ? -1 : ((o1.ary1 == o2.ary1) ? 0 : 1)); });
for (var i=0; i < merged.length; i++) { ary1[i] = merged[i].ary1; ary2[i] = merged[i].ary2; }
}
var ary1 = new Array("d", "a", "b", "c");
var ary2 = new Array("ee", "rr", "mm", "yy");
console.log(ary1);
console.log(ary2);
sortBoth(ary1, ary2);
console.log(ary1);
console.log(ary2);
Output: 输出:
[ "d", "a", "b", "c"]
["ee", "rr", "mm", "yy"]
[ "a", "b", "c", "d"]
["rr", "mm", "yy", "ee"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.