0

I have a class:

   class Rates{
var price:Double?
}

Multidimensional Array:

var rates:[[Rates]] = [[],[],[],[],[]]

I am trying to sort every array in rates from smallest Double to Largest. Here is what I tried

 for x in 0 ..< 4 {

      self.shippingRates[x].sort({ (Rates, Rates2) -> Bool in
                            return Rates.price < Rates2.price
                        })
                    }

But for some reason the sort isn't working, when i display the data it doesn't show up sorted. What am I doing wrong?

2
  • none of them are being sorted, i did < rates.count as well Commented Jul 29, 2016 at 3:17
  • Edited the post, I had rates2.price in the code :/ still doesnt sort Commented Jul 29, 2016 at 3:21

1 Answer 1

1

Because sort returns a new array, which you didn't retain. Use sortInPlace instead:

for x in 0 ..< 4 {
    self.shippingRates[x].sortInPlace { $0.price < $1.price }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.