Suppose I have a list of things (numbers, to keep things simple here) and I have a function I want to use to sort them by, using SortBy. For example, the following sorts a list
There is a variant of SortBy which breaks ties by using additional ordering functions:
SortBy
SortBy[list,{f1, f2, ...}]
By counting ties you can thus obtain a stable sorting:
Module[{tie = 0}, SortBy[{19, 301, 201, 502, 501, 101, 300}, {Mod[#, 10] &, (tie++) &}]]
yields
{300, 301, 201, 501, 101, 502, 19}