+ 3

JS sort one array based on another array’s sortation

arrA = [8, 6, 7] // B follows this arr (A) arrB = [‘h’, ‘u’, ‘z’] // B follows A arrA.sort() // output: 6, 7, 8 // arrB.followA’s arrangement somehow // output: u, z, h arrA.reverse() // output: 8, 7, 6 // arrB.follow A’s arrangement // output: h, z, u Code that I started: https://code.sololearn.com/WVQeIKjN26SE/?ref=app

29th Nov 2020, 2:52 PM
Ginfio
Ginfio - avatar
4 Antworten
+ 2
Let combinedArr = arrA.map((el,i)=> [el, arrB[i]]) Then sort based on the first item of the sub arrays of the variable "combinedArr" (sort() accept a call back function) then separate them using filter (number or letters?) Into two new variables (or the same old variables a and b if you want) and you are done
29th Nov 2020, 4:46 PM
Ali Kh
Ali Kh - avatar
+ 2
Feel free to ask for clarification
29th Nov 2020, 5:28 PM
Ali Kh
Ali Kh - avatar
+ 1
var arrA = [6, 8, 7]; var arrB = ['h', 'u', 'z'] let combinedArr = arrA.map((el,i)=> [el, arrB[i]]) console.log(combinedArr); combinedArr.sort((a,b)=> a[0] - b[0]); // sorting based on the number arrA = combinedArr.flat().filter(el=> typeof el == "number"); arrB = combinedArr.flat().filter(el=> typeof el == "string") console.log(arrA , arrB )
29th Nov 2020, 5:23 PM
Ali Kh
Ali Kh - avatar
0
Ali Kh is it aight if u walk me through.. I got this so far, don’t understand what’s next. (even though u said it in ur comment above) https://code.sololearn.com/WV5RWyr0O40r/?ref=app
29th Nov 2020, 5:16 PM
Ginfio
Ginfio - avatar