sorting function to help with duplicate removal