Sort struct by alphabetical order then descending int order

My code below sorts the struct in alphabetical order. However I would like code the code to be sort in alphabetical order first then by descending int order. For example joe, 3; joe 4.

      (contacts.sorted {$0.description < $1.description}).map {$0.description}.joined(separator: "\n")

This is what I tried but it is not working.

   { $0.name < $1.name && $1.phone > $0.phone }

Swift allows you to use tuples for this kind of thing. I love it.

sorted { 
  ($0.name, $0.phone)
  < 
  ($1.name, $1.phone)
}

If that’s not what you’re after, you can reorder the $0 and $1 as appropriate.

I think you have “ascending” and “descending” backwards in your mind. This is the 3rd or 4th post I’ve seen from you where you say you want descending, but then you show ascending. Having the smaller number before the bigger number means it’s ascending order.

This topic was automatically closed after 166 days. New replies are no longer allowed.