[英]How to sort 2nd array by sorting the 1st one using Javascript or JQuery?
I have 2 array. 我有2个数组。 I want to sort only first array and the second array should be shorted on the base of first array. 我只想对第一个数组排序,而第二个数组应该在第一个数组的基础上缩短。
Before sorting: 排序之前:
arrTexts = ["Company", "Department", "Account"];
arrValue = ["nameCompany", "department", "account"];
Should be like this after shorting: 短路后应该是这样的:
arrTexts = ["Account", "Company", "Department"];
arrValue = ["account", "nameCompany", "department"];
I know sorting by arrTexts.sort();
我知道通过arrTexts.sort();
排序arrTexts.sort();
But it is not so useful in above case. 但这在上述情况下不是那么有用。
Please not array length can be 300+. 请不要将数组长度设为300+。 So, performance(sorting speed) also maters. 因此,性能(排序速度)也很重要。
Can anybody please suggest me? 有人可以建议我吗?
Here is some code to get started. 这是一些入门代码。
var arrTexts = [ "Company", "Department", "Account" ]; var arrValue = [ "nameCompany", "department", "account" ]; var sortedArr1 = arrTexts.sort(); var sortedArr2 = []; sortedArr1.forEach(function(v, i) { var t = arrValue.find(function(_v) { return _v.toLowerCase().indexOf(v.toLowerCase()) > -1; }); sortedArr2.push(t); }); console.log(sortedArr1); console.log(sortedArr2);
Here is a way to do it : 这是一种方法:
arrTexts = ["Company", "Department", "Account"];
arrValues = ["nameCompany", "department", "account"];
arrTextsValues = {};
arrTexts.forEach(function(item, key){
arrTextsValues[item] = arrValues[key];
})
arrTextsSorted = arrTexts.sort();
arrValuesSorted = [];
arrTextsSorted.forEach(function(item){
arrValuesSorted.push(arrTextsValues[item]);
})
console.log(arrTextsSorted, arrValuesSorted);
It outputs this : 它输出以下内容:
[ 'Account', 'Company', 'Department' ]
[ 'account', 'nameCompany', 'department' ]
First I create an object that will hold a correspondance between texts and values, then I sort the texts and finally, I loop over the sorted texts to create an array holding the values in the correct order based of the correspondance object created earlier. 首先,我创建一个对象,该对象将保持文本和值之间的对应关系,然后对文本进行排序,最后,遍历已排序的文本,以根据先前创建的对应对象以正确的顺序创建一个数组,以正确的顺序保存值。
Hope this helps. 希望这可以帮助。
I don't know if it will suits your needs regarding performance issues. 我不知道它是否适合您的性能问题。 That's yours to find out :) 那是你的发现:)
You could use the indices as a temporary array and sort it with the values of arrTexts
. 您可以将索引用作临时数组,并使用arrTexts
的值对其进行arrTexts
。 Then map the result to arrValue
. 然后将结果映射到arrValue
。
var arrTexts = ["Company", "Department", "Account"], arrValue = ["nameCompany", "department", "account"], indices = arrTexts.map(function (_, i) { return i; }), values, texts; indices.sort(function (a, b) { return arrTexts[a].localeCompare(arrTexts[b]); }); texts = indices.map(function (i) { return arrTexts[i]; }); values = indices.map(function (i) { return arrValue[i]; }); console.log(values); console.log(texts);
var arrTexts = ["Company", "Department", "Account"]; var arrValue = ["nameCompany", "department", "account"]; // temporary array holds objects with position and sort-value var mapped = arrTexts.map(function(el, i) { return { index: i, value: el }; }) // sorting the mapped array containing the reduced values mapped.sort(function(a, b) { return +(a.value > b.value) || +(a.value === b.value) - 1; }); // container for the resulting order var result = mapped.map(function(el){ return arrValue[el.index]; }); console.log(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.