简体   繁体   English

如何通过使用Javascript或JQuery对第一个数组进行排序来对第二个数组进行排序?

[英]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.

相关问题 有没有办法使用第一个数组作为 Javascript 中的参考在第二个数组中获取相应的索引值? - Is there a way to get corresponding index value in a 2nd array using the 1st array as a reference in Javascript? 通过使用第二个数组(JavaScript)中的引用从第一个数组中的每个 object 中提取每个值来创建一个新数组 - Make a new array by extracting each value from each object in the 1st array using the references in the 2nd array (JavaScript) 将段落按顺序追加到image属性? (第1至1,第2至2…第n至nth)JQUERY - Appending paragraphs to an image attribute in order? (1st to 1st, 2nd to 2nd … nth to nth) JQUERY 第一个单词第一个字符第二个单词第二个字符直到数组结尾 - 1st word 1st character 2nd word 2nd character and on till the end of array 检查数组中的匹配键,如果找到,则取第二个数组条目,如果在 javascript 中不取第一个数组条目 - Check for matching key in array, if found take 2nd array entries if not take 1st array entries in javascript jQuery一种衬板,用于查找和突出显示第一个单元格为gte第二个单元格的行 - jQuery one liner for finding and highlighting rows where contents of 1st cell is gte 2nd cell 如何使用 GAS 将表单响应 object 转换为二维数组,其中第一行是键/标题,第二行是值/答案? - How to turn form responses object into a 2D array with the 1st row being the key/header and the 2nd the values/answers using GAS? 在第一个数组元素中的第二个数组元素中查找字母 - Finding letters in 2nd array element in 1st array element 如果第二个数组匹配,则对第一个数组中的数字求和 - Summing numbers from 1st array if 2nd array match jQuery:根据第一个下拉菜单的选择更改第二个下拉菜单的选择 - jQuery: Change selection of 2nd dropdown based on selection of 1st
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM