update sort logic to handle empty containers