# Iteratively sort a list

Given a list of Integers greater than zero, Sort each of the unique values in ascending order, then repeat the process on the remaining duplicate values and append.

Example:

[1,5,2,2,8,3,5,2,9] ==> [1,2,3,5,8,9,2,5,2]
[8,5] ==> [5,8]
[2,2,2] ==> [2,2,2]

• You should add more test inputs, including "corner" cases such as [4 4 4] and [8 5], as well as [] if the empty input need to be handled Dec 12, 2023 at 23:43
• Closely related: Simulate Round Robin Scheduling Dec 13, 2023 at 4:37
• added extra cases Dec 13, 2023 at 15:13
• I read this first as cynically sort a list, and was really curious. Still am, but in a different way.
– Scot
Dec 13, 2023 at 23:16
• @DomHastings yeah, i was just lazy typing this out Dec 22, 2023 at 16:30

# C# (Visual C# Compiler), 317 bytes

using System.Linq;class P{static void Main() => System.Console.WriteLine(string.Join(", ", S(new ushort[] { 1, 5, 2, 2, 8, 3, 5, 2, 9 }))); static ushort[] S(ushort[] a) => a.Select(v => o[v] = (ushort)(o[v] + 256 | v)).OrderBy(x => x).Select(v => (ushort)(v & 255)).ToArray(); static ushort[] o = new ushort[65536];}


Try it online!

• 154 bytes Jan 29 at 23:28

# Julia 1.0, 56 53 bytes

~a=a>[] ? [sort!(a)[(d=diff([0;a])).>0];~a[d.<1]] : a


Try it online!

sort the array, take each first occurence with diff, and recurse