Relative Sort Array

Given two arrays arr1 and arr2, sort arr1 so that the relative ordering of elements appearing in arr2 comes first, followed by the remaining elements in ascending order. A custom comparator is the natural tool for this ordering rule.

Input Format

arr1 = array to sort, arr2 = order reference array

Output Format

arr1 sorted by the custom relative order

Constraints

  • 1 <= arr1.length, arr2.length <= 1000; 0 <= arr1[i], arr2[i] <= 1000

Examples

Example 1:

Input:

arr1 = [2,3,1,3,2,4,6,7,9,2,19]
arr2 = [2,1,4,3,9,6]

Output:

[2,2,2,1,4,3,3,9,6,7,19]

Explanation:

Elements in arr2 keep their relative order, and the rest are appended in ascending order.

Example 2:

Input:

arr1 = [28,6,22,8,44,17]
arr2 = [22,28,8,6]

Output:

[22,28,8,6,17,44]

Explanation:

Numbers from arr2 come first in that order.

Loading...
Relative Sort Array