fix bug where `sort` mutated its input list

review/merge by @TheBizzle plz