> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Relative Sort Array Python Solution with Tests

> Tested Python solution for LeetCode 1122 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 1122, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), Counting Sort, [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/relative-sort-array/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1122   # by problem number
lcpy gen -s relative_sort_array   # by problem name
```

## Problem

Given two arrays \<code>arr1\</code> and \<code>arr2\</code>, the elements of \<code>arr2\</code> are distinct, and all elements in \<code>arr2\</code> are also in \<code>arr1\</code>.

Sort the elements of \<code>arr1\</code> such that the relative ordering of items in \<code>arr1\</code> are the same as in \<code>arr2\</code>. Elements that do not appear in \<code>arr2\</code> should be placed at the end of \<code>arr1\</code> in \<strong>ascending\</strong> order.

### Examples

```
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]
```

```
Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]
Output: [22,28,8,6,17,44]
```

### Constraints

* \<code>1 \<= arr1.length, arr2.length \<= 1000\</code>
* \<code>0 \<= arr1\[i], arr2\[i] \<= 1000\</code>
* All the elements of \<code>arr2\</code> are \<strong>distinct\</strong>.
* Each \<code>arr2\[i]\</code> is in \<code>arr1\</code>.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/relative_sort_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/relative_sort_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def relative_sort_array(self, arr1: list[int], arr2: list[int]) -> list[int]:
        rank = {v: i for i, v in enumerate(arr2)}
        present = sorted((x for x in arr1 if x in rank), key=lambda x: rank[x])
        rest = sorted(x for x in arr1 if x not in rank)
        return present + rest
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.