edited sortByTime function