-1
var array = [["Sally", "3", "blue"], ["Jack", "6", "green"], ["Harold", "2", "yellow"]]

I am trying to order the contents of this [[String]] based on the first index of each value (ordering by the names; Sally, Jack, Harold).

Is there a Swift 3 method that I can use to order these automatically? Or, is there some loop I can create that will iterate through these inner arrays to sort them based on the names of the people?

EvanOgo
  • 541
  • 1
  • 4
  • 11

1 Answers1

0

Yes, as simple as it can be:

var array = [["Sally", "3", "blue"], ["Jack", "6", "green"], ["Harold", "2", "yellow"]]

array.sort { $0.first! < $1.first! } // [["Harold", "2", "yellow"], ["Jack", "6", "green"], ["Sally", "3", "blue"]]
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • 2
    It'd be nice if you explained the meaning of `$0` and `$1`, as not everyone may know it. – Losiowaty Mar 03 '17 at 22:12
  • 4
    ... Or flagging this question as a duplicate. – JAL Mar 03 '17 at 22:13
  • My apologies, I asked this question because I was having troubles with solutions from other questions. But when I checked over my code I found out it was something on my end. Nonetheless this solution now works fine. Thank you very much! – EvanOgo Mar 03 '17 at 22:19