Sorting dictionary in O(n)