Skip to content
Advertisement

Sort List with duplicates based on related list

Shortened the scenario for brevity.

I have three arrays

JavaScript

where there is a relationship between the elements at i-th position between the three arrays..

for eg A1[0] is related to A2[0] and also A3[0] and so on …

I want to sort the three arrays BASED ON THE A2 (in ascending order).

so after sorting, the arrays become

JavaScript

One thing I am not able to figure out is while sorting, if there is a duplicate record in A2 (20 in this case), then the sort should take the value in A2 which has a lesser corresponding value in A3.. Thats is why A should come before E in the final A1 list.

Any help would be appreciated.

As of now, I am trying to do this using quick sort, Please find my code below

JavaScript

Please note: I have to do this without using inbuilt functions

Advertisement

Answer

JavaScript

And since you need it without built-in functions:

JavaScript

Resulting in

JavaScript

Note: I took some shortcuts in my reimplementation of the built-ins. Their exception handling is less than ideal, some features are missing, and the behavior isn’t exactly the same, but they work for their intended purposes.

Note 2: range() and print() were not reimplemented.

Advertisement