6 kyu

2 Arrays 1 Sort

143 of 297IVBakker

Description:

Imagine two arrays/lists where elements are linked by their positions in the array. For example:

HowMany = [ 1   ,   6  ,  5  ,   0  ];
Type = ['house', 'car','pen','jeans'];

Means I have 1 house, 6 cars,5 pens and 0 jeans.

Now if we sort one array we lose the connectivity. The goal is to create a sorting function that keeps the position link linkedSort(arrayToSort,linkedArray,compareFunction). So for every element that moves in arrayToSort(HowMany in the example), the corresponding element in linkedArray(Type in the example) needs to move similarly.

For example in Javascript:

//INPUT
HowMany = [ 1   ,   6  ,  5  ,   0  ];
Type = ['house', 'car','pen','jeans'];

//SORT
res = linkedSort(HowMany,Type,function(a,b){return a-b;})

//OUTPUT
HowMany === res === [ 0   ,   1   ,  5  ,  6  ];
Type       ===     ['jeans','house','pen','car'];

linkedSort(...) return the "arrayToSort" sorted only.

If no compare function is provided you should handle like an alphabetical sorting would do, e.g:

[-71,-6,35,0].sort() ===  [-6,-71,0,35] != [-71,-6,0,35]
yoursortingfunction([-71,-6,35,0]) ===  [-6,-71,0,35] != [-71,-6,0,35]
yoursortingfunction([-71,-6,35,0]) ===  [-6,-71,0,35] != [-71,-6,0,35]

Note: it is assumed that array are same length.

Arrays
Algorithms
Sorting

Stats:

CreatedNov 18, 2014
PublishedNov 18, 2014
Warriors Trained1255
Total Skips42
Total Code Submissions2694
Total Times Completed297
JavaScript Completions151
Ruby Completions24
Python Completions143
Total Stars40
% of votes with a positive feedback rating80% of 131
Total "Very Satisfied" Votes92
Total "Somewhat Satisfied" Votes26
Total "Not Satisfied" Votes13
Total Rank Assessments27
Average Assessed Rank
5 kyu
Highest Assessed Rank
4 kyu
Lowest Assessed Rank
7 kyu
Ad
Contributors
  • IVBakker Avatar
  • GiacomoSorbi Avatar
  • JohanWiltink Avatar
  • FArekkusu Avatar
Ad