Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Relative sorting in JavaScript
Suppose, we have two arrays, let's say arr1 and arr2. The elements of arr2 are distinct, and all elements in arr2 are also in arr1.
We are required to write a JavaScript function that takes in two such arrays and sorts the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.
For example? If the two input arrays are ?
const arr1 = [2,3,1,3,2,4,6,7,9,2,19]; const arr2 = [2,1,4,3,9,6];
Then the output should be ?
[2,2,2,1,4,3,3,9,6,7,19]
Algorithm Approach
The solution uses a Map to store the priority order of elements from arr2. For elements not in arr2, we assign them a higher priority value and sort them in ascending order at the end.
Example
const arr1 = [2,3,1,3,2,4,6,7,9,2,19];
const arr2 = [2,1,4,3,9,6];
const relativeSortArray = (arr1, arr2) => {
const map = new Map();
const len = arr2.length;
// Create priority map from arr2
arr2.forEach((a, i) => {
map.set(a, i);
});
// Sort based on priority
return arr1.sort((a, b) => {
a = map.has(a) ? map.get(a) : len + a;
b = map.has(b) ? map.get(b) : len + b;
return a - b;
});
};
console.log(relativeSortArray(arr1, arr2));
[ 2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19 ]
How It Works
1. Priority Mapping: Elements in arr2 get priority indices (0, 1, 2, ...)
2. Higher Priority for Others: Elements not in arr2 get priority as len + value
3. Sorting: Lower priority values come first, maintaining relative order
Step-by-Step Breakdown
const arr1 = [2,3,1,3,2,4,6,7,9,2,19];
const arr2 = [2,1,4,3,9,6];
// Priority map: {2?0, 1?1, 4?2, 3?3, 9?4, 6?5}
// Element 7: priority = 6 + 7 = 13
// Element 19: priority = 6 + 19 = 25
console.log("Priority mapping:");
const map = new Map();
arr2.forEach((a, i) => map.set(a, i));
console.log("Elements in arr2:", [...map.entries()]);
console.log("Elements not in arr2 get priority: len + value");
Priority mapping: Elements in arr2: [ [ 2, 0 ], [ 1, 1 ], [ 4, 2 ], [ 3, 3 ], [ 9, 4 ], [ 6, 5 ] ] Elements not in arr2 get priority: len + value
Alternative Approach
const relativeSortArrayAlternative = (arr1, arr2) => {
const result = [];
const remaining = [];
// Add elements in arr2 order
arr2.forEach(num => {
const matches = arr1.filter(x => x === num);
result.push(...matches);
});
// Add remaining elements in sorted order
arr1.forEach(num => {
if (!arr2.includes(num)) {
remaining.push(num);
}
});
return [...result, ...remaining.sort((a, b) => a - b)];
};
const arr3 = [2,3,1,3,2,4,6,7,9,2,19];
const arr4 = [2,1,4,3,9,6];
console.log(relativeSortArrayAlternative(arr3, arr4));
[ 2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19 ]
Conclusion
Relative sorting maintains the order specified in a reference array while placing unmatched elements at the end. The Map-based approach provides an efficient O(n log n) solution using custom comparison logic.
