11

I found these answers:

Sort an array of tuples in swift 3 How to sort an Array of Tuples?

But I'm still having issues. Here is my code:

var countsForLetter:[(count:Int, letter:Character)] = []
...
countsForLetter.sorted(by: {$0.count < $1.count})

Swift 3 wanted me to add the by: and now it says that the result of the call to sorted:by is unused.

I'm new to swift 3. Sorry if this is a basic question.

Community
  • 1
  • 1
Thom
  • 14,013
  • 25
  • 105
  • 185

2 Answers2

24

You are getting that warning because sorted(by... returns a new, sorted version of the array you call it on. So the warning is pointing out the fact that you're not assigning it to a variable or anything. You could say:

countsForLetter = countsForLetter.sorted(by: {$0.count < $1.count})

I suspect that you're trying to "sort in place", so in that case you could change sorted(by to sort(by and it would just sort countsForLetter and leave it assigned to the same variable.

creeperspeak
  • 5,403
  • 1
  • 17
  • 38
8

Sorted() returns a new array, it does not sort in place.

you can use :

countsForLetter  = countsForLetter.sorted(by: {$0.count < $1.count})

or

countsForLetter.sort(by: {$0.count < $1.count})
Alain T.
  • 40,517
  • 4
  • 31
  • 51