Since Ruby's sort_by method is not stable, use a modified sort_by method that breaks ties using the original index position of the array member.