|
I don't really understand how .sorted(by: ) works. I followed Paul's example from Day 9 but I still don't get it. I see what he did and I can manipulate it to do similar things and with .map and .filter, i can use closures and make them do what I want and it seems to all make sense but .sorted(by: ) isn't clicking with me. Here's his example:
I was able to use his code and do similar sorts with .hasPrefix() and .hasSuffix() and that all worked, but I don't understand why. I also tried just messing around with .sorted(by: ) and I'm not really understanding the output I'm getting or how it's all working. I tried this:
|
|
Here's an example. Say you gather your family around the dining room table. You dump a box of fruit on the table, and you ask each person to "sort" the fruit. This may not make sense to everyone, as there is no prescribed way to "sort" fruit. Instead, you give each person a sorting rule.
Each is performing a sort function, but you had to specify the sort rules. When comparing two different fruits, what is the rule to determine weight? What is the rule for sorting color? This is the function of the closure you supply to the sorted(by:) method. Swift will give the closure two of your fruits (say the banana and the peach. Or the pomegranite and the plum.) It doesn't matter which two fruits it receives, you need to provide the rules to determine if fruit #1 is ordered before fruit #2. The beautiful thing about closures is you can easily change the sorting rules based on your needs! In this example below. Think through what you are asking Bjorn to do? You give him two names ("Piper and Gloria", or "Tiffany and Suzanne"). But no matter what the names are, you're just saying returning true. This essentially tells the sort routine that name1 will always be sorted before name2. You're not comparing anything. To make this more clear, you might want to add a print statement inside and watch the console.
Here's a second example with some better sorting logic. Sort the list of names by how many letters are in each name. Run this in Playgrounds. Do you see how the sortby rule is applied?
|
|
Ok, thanks. By putting the print statements in there I was able to see exactly what it was doing and now it makes sense. I'll probably forget it five minutes from now but at least I can go back and understand it. Thanks, again. |
|
My suggestion about forgetting..... Create a new playground for Swift concepts. You can create many pages in a single Playground. Create a new page to cover specific concepts. Put sample code in each and add a lot of documentation to your future self. For example one page might simply be titled: Array_SortBy Then, in future, you can see working code, your notes, and have a quick way to reinforce concepts. |
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and more!
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.