Created
December 17, 2019 14:40
-
-
Save gkucmierz/cbfd265a699b3f36c609453289363f49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // sort first array using another array | |
| // - both arrays are same length | |
| // - sort is stable | |
| function sortUsing(a1, a2) { | |
| const a = []; | |
| for (let i = 0; i < a1.length; ++i) { | |
| a[i] = [a1[i], a2[i], i]; | |
| } | |
| return a.sort((a, b) => { | |
| const d = a[1]-b[1]; | |
| return d === 0 ? a[2]-b[2] : d; | |
| }).map(a => a[0]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment