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.

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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})

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.