improved sort functionality