fixing the sorting function by adding the limit