Working sort code.