[Java/Python 3] Convert and sort indices accordingly.
Q & A\nQ: How is Arrays.sort(indices, Comparator.comparing(i -> convert(nums[i], mapping))); sorting the elements of indices array?\n\nA: Sort the i\'s in indic
**Q & A**\n*Q*: How is `Arrays.sort(indices, Comparator.comparing(i -> convert(nums[i], mapping)));` sorting the elements of `indices` array?\n\n*A*: Sort the `i`\'s in `indices` according to the corresponding value of `convert(nums[i], mapping)`. E.g.,\n\nInput: mapping = [8,9,4,0,2,1,3,5,7,6], nums = [991,338,38]\n\nThen we need to sort `indices = [0, 1, 2]`.\n\nExplanation: \nMap the number 991 as follows:\n1. mapping[9] = 6, so all occurrences of the digit 9 will become 6.\n2. mapping[1] = 9, so all occurrences of the digit 1 will become 9.\nTherefore, the mapped value of 991 is 669, and **index `0` is mapped to 669.**\n\n338 maps to 007, or 7 after removing the leading zeros, and **index `1` is mapped to 7**.\n38 maps to 07, which is also 7 after removing leading zeros, and **index `2` is mapped to 7**..\nSince 338 and 38 share the same mapped value, they should remain in the same relative order, so 338 comes before 38, and **index 1 should come before index 2**.\n\n**So the sorted indices is [1, 2, 0], and the corresponding values in nums are 338, 38, 991.**\n\n\nThus, the sorted array is [338,38,991].\n\n\n**End of Q & A**\n\n\n----\n\n\nSort the indices by the converted value first, if equal, then by original index.\n\nBoth java.util.Arrays.sort(T[] a, Comparator<? super T> c) and sorted() in Python are guaranteed to be stable, so no need to sub-sort by the index. -- credit to **@SunnyvaleCA**.\n\n```java\n    public int[] sortJumbled(int[] mapping, int[] nums) {\n        int n = nums.length, k = 0;\n        Integer[] indices = new Integer[n];\n        for (int i = 0; i < n; ++i)\n            indices[i] = i;\n        Arrays.sort(indices, Comparator.comparing(i -> convert(nums[i], mapping)));\n     // return Stream.of(indices).mapToInt(i -> nums[i]).toArray();\n        int[] ans = new int[n];\n        for (int idx : indices)\n            ans[k++] = nums[idx];\n        return ans;\n    }\n    private int convert(int num, int[] mapping) {\n        if (num == 0)\n            return mapping[0];\n        int n = 0, f = 1;\n        while (num > 0) {\n            n += mapping[num % 10] * f;\n            f *= 10;\n            num /= 10;\n        }\n        return n;\n    }\n```\n\n----\n\n\n\n```python\n    def sortJumbled(self, mapping: List[int], nums: List[int]) -> List[int]:\n        \n        def convert(num: int) -> int:\n            if num == 0:\n                return mapping[0]\n            n, f = 0, 1\n            while num > 0:\n                num, r = divmod(num, 10)\n                n += mapping[r] * f\n                f *= 10\n            return n\n        \n        indices = sorted(range(len(nums)), key=lambda i: convert(nums[i]))\n        return [nums[i] for i in indices]\n```\n**Analysis:**\n\nTime: `O(nlogn)`, space: `O(n)`, where `n = nums.length`.\n\n-------\n\nPlease **upvote** if the post is helpful.
['Java', 'Python3']