-1

I have 2 array. 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(); But it is not so useful in above case.

Please not array length can be 300+. So, performance(sorting speed) also maters.

Can anybody please suggest me?

chhaya_patel
  • 171
  • 1
  • 2
  • 15

4 Answers4

2

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);
Pugazh
  • 9,453
  • 5
  • 33
  • 54
1

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 :)

vincenth
  • 1,732
  • 5
  • 19
  • 27
1

You could use the indices as a temporary array and sort it with the values of arrTexts. Then map the result to 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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

   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);
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30