sort and apply based on whatver is passed