|
The use case I have is driving me to a sort that depends on over 30 criteria and as they would almost never all be needed I suspect the performance impact is not large (usually the first criterion is all that would be evaluated). My question is whether there is a powerful syntax the lets me avoid this:
|
|
Rather than create some massive What does it mean for two items of whatever type
you can simply do this:
because the items in It's kind of hard to say anything more specific without seeing some sample data and examples of how they should sort. |
|
Thanks again! I woke up this morning with the idea of needing my own sort method, but was not sure if I could just paste it in the closure... Creating the func < certainly looks like the answer. |
|
FYI, when I created my own sort method func <, I learned something interesting about the way the simple sorting closure works with multiple criteria :
The syntax above only says what to do when $0 is < $1 for example but in fact, when there are multiple criteria it has to deal with <, = and > so when I first implemented my sort method I assumed I could check for < and treat the = and > cases as just not < but it affects the way the secondary sort gets used of course so it has to be more explicit than in the simple closure syntax. I ended up with:
|
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until February 9th.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.