Sort by Second DigitEasy

Sort by Second Digit

EasyPractice library · +10 XPsortingcustom sort

Given a list of N integers, sort them in ascending order based on their second digit. If two numbers have the same second digit, maintain their relative order from the original list (stable sort). If a number has only one digit, consider its second digit to be 0.

Input

The first line contains an integer N (1 <= N <= 1000). The second line contains N space-separated integers, a_1, a_2, ..., a_N.

Output

Print the N integers sorted according to the rule, separated by spaces.

Constraints

1 <= N <= 1000 1 <= a_i <= 10^9

Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
5
123 45 6 789 10
Output
6 10 123 45 789

Sorted by second digit: 0 (from 6, 10), 2 (from 123), 5 (from 45), 8 (from 789). Original relative order for 6 and 10 is preserved.

Example 2

Input
4
99 88 77 66
Output
66 77 88 99

All numbers have the same second digit (9, 8, 7, 6). The list is sorted based on these digits. The relative order of numbers with same second digit is maintained.

Hints