1

These are my two arrays. I want sizeIdArray to get sorted on the basis of sizesNameArray. For Example,

sizeIdArray = [1, 2, 3];
sizeNameArray = [2.5m, 8m, 3.5m];

After sorting sizeNameArray,

sizeNameArray = [2.5m, 3.5m, 8m];

and

sizeIdArray should be [1, 3, 2];

How do i attain this? I was able to obtain the sizeNameArray by this method.

sizesNameArray.sort(function(a,b){return parseFloat(a.replace('#', ''))-parseFloat(b.replace('#', ''))})

replace function is if there was '#' in front.

Sorry if i am not clear with my question. I want to sort both at the same time, but sizeIdArray should be sorted according to sizeNameArray.

Please help. Thank You

Shah Rukh
  • 291
  • 1
  • 10

4 Answers4

1

sizeIdArray = [1, 2, 3];
sizeNameArray = ['2.5m', '8m', '3.5m'];

//1) combine the arrays:
var list = [];
for (var j = 0; j < sizeNameArray.length; j++) 
    list.push({'sizeId': sizeIdArray[j], 'sizeName': sizeNameArray[j]});

//2) sort:
list.sort(function(a, b) {
    return ((a.sizeName.replace('#', '') < b.sizeName.replace('#', '')) ? -1 
            : ((a.sizeName.replace('#', '') == b.sizeName.replace('#', '')) ? 0 : 1));
    //Sort could be modified too, for example sort on the id if the name is the same.
});

//3) separate them back out:
for (var k = 0; k < list.length; k++) {
    sizeNameArray[k] = list[k].sizeName;
    sizeIdArray[k] = list[k].sizeId;
}

console.log(sizeIdArray);
console.log(sizeNameArray);

Refer Sort two arrays the same way

mangesh
  • 511
  • 8
  • 24
1

Heres more general approach, no need of any parsefloat.

sizeIdArray = [1, 2, 3];
sizeNameArray = ['2.5m', '8m', '3.5m'];
var m = {}
for(let i in sizeNameArray){
   m[sizeNameArray[i]] = sizeIdArray[i]
}


Object.keys(m).sort().forEach((item,index)=>sizeIdArray[index] = m[item])
console.log(sizeIdArray)
Shiva Sai
  • 463
  • 4
  • 12
1

This is not the perfect solution but if time complexity is not a concern, you can use this algorithm. It's sorting it manually by finding the smallest number in each iteration and swapping it. But, when you swap the sizeNameArray you also swap the sizeIdArray and hence you will get the result needed. To make it more optimal you can refer to this Sort an Array based on Another Array

sizeIdArray = [1, 2, 3];
sizeNameArray = [2.5, 8, 3.5];

for(let i = 0; i < sizeNameArray.length; i++) {
  let smallest = i;
  for(let j = i; j < sizeNameArray.length; j++) {
    if(sizeNameArray[j] < sizeNameArray[smallest])
      smallest = j;
  }
  let temp = sizeNameArray[i];
  sizeNameArray[i] = sizeNameArray[smallest];
  sizeNameArray[smallest] = temp;
  
  temp = sizeIdArray[i];
  sizeIdArray[i] = sizeIdArray[smallest];
  sizeIdArray[smallest] = temp;
}

console.log(sizeIdArray);
keidakida
  • 713
  • 4
  • 12
0

I did it like this.

var done = false;
      while (!done) {
        done = true;
        for (var i = 1; i < sizesNameArray.length; i += 1) {
          if (parseFloat(sizesNameArray[i - 1].replace('#', '')) > parseFloat(sizesNameArray[i].replace('#', ''))) {
            done = false;
            var tmp = sizesNameArray[i - 1];
            var tmp2 = sizeIdArray[i-1];
            sizesNameArray[i - 1] = sizesNameArray[i];
            sizeIdArray[i - 1] = sizeIdArray[i];
            sizesNameArray[i] = tmp;
            sizeIdArray[i] = tmp2;
          }
        }
      }
Shah Rukh
  • 291
  • 1
  • 10