> ## 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.

# Sort the Jumbled Numbers Python Solution

> Tested Python solution for LeetCode 2191 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 2191, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/sort-the-jumbled-numbers/description/).

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

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

## Problem

You are given a \<strong>0-indexed\</strong> integer array \<code>mapping\</code> which represents the mapping rule of a shuffled decimal system. \<code>mapping\[i] = j\</code> means digit \<code>i\</code> should be mapped to digit \<code>j\</code> in this system.

The \<strong>mapped value\</strong> of an integer is the new integer obtained by replacing each occurrence of digit \<code>i\</code> in the integer with \<code>mapping\[i]\</code> for all \<code>0 \<= i \<= 9\</code>.

You are also given another integer array \<code>nums\</code>. Return \<em>the array \</em>\<code>nums\</code>\<em> sorted in \<strong>non-decreasing\</strong> order based on the \<strong>mapped values\</strong> of its elements.\</em>

\<p>\<strong>Notes:\</strong>\</p>

\<ul>
\<li>Elements with the same mapped values should appear in the \<strong>same relative order\</strong> as in the input.\</li>
\<li>The elements of \<code>nums\</code> should only be sorted based on their mapped values and \<strong>not be replaced\</strong> by them.\</li>
\</ul>

### Examples

```
Input: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]
Output: [338,38,991]
Explanation:
Map the number 991 as follows:
1. mapping[9] = 6, so all occurrences of the digit 9 will become 6.
2. mapping[1] = 9, so all occurrences of the digit 1 will become 9.
Therefore, the mapped value of 991 is 669.
338 maps to 007, or 7 after removing the leading zeros.
38 maps to 07, which is also 7 after removing leading zeros.
Since 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38.
Thus, the sorted array is [338,38,991].
```

```
Input: mapping = [0,1,2,3,4,5,6,7,8,9], nums = [789,456,123]
Output: [123,456,789]
Explanation: 789 maps to 789, 456 maps to 456, and 123 maps to 123. Thus, the sorted array is [123,456,789].
```

### Constraints

* mapping.length == 10
* 0 \<= mapping\[i] \<= 9
* All the values of mapping\[i] are unique.
* 1 \<= nums.length \<= 3 \* 10^4
* 0 \<= nums\[i] \< 10^9

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n * d) where d is the digit count
    # Space: O(n)
    def sort_jumbled(self, mapping: list[int], nums: list[int]) -> list[int]:
        return sorted(nums, key=lambda num: int("".join(str(mapping[int(d)]) for d in str(num))))
```

## Complexity

| Time | Space |
| - | - |
| O(n log n \* d) where d is the digit count | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


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