UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Trying to understand usage of improved(?) sorting method in Project 14 - BucketList (Days 71, 72)

Forums > 100 Days of SwiftUI

The Day 71 page provides instructions for downloading the data from Wikipedia. Supposedly, the results of the web search aren't alphabetized, but they end up alphabetized when I run the app. Day 72 then provides a sorting method. I'm not sure why we should make the change--maybe it's considered easier to change the method later on if one were to decide to sort in a different fashion??

Day 71: https://www.hackingwithswift.com/books/ios-swiftui/downloading-data-from-wikipedia

pages = items.query.pages.values.sorted { $0.title < $1.title }

Day 72: https://www.hackingwithswift.com/books/ios-swiftui/sorting-wikipedia-results

pages = items.query.pages.values.sorted() + extra code

1      

You can think of dozens of examples of sorting objects with a natural sort order.

Integers? Sort by value, naturally.
Utensils? Sort them in your drawer by forks, knives, and spoons
Bedroom closet? Jackets, Shirts, Trousers
Cash Register? Ones, Fivers, Tenners

What about albums? Sort by Artist? or by Genre? What's the natural order?

// This is a one time rule telling swift to sort Wiki articles by Title.
        pages = items.query.pages.values.sorted { $0.title < $1.title }

// This tells Swift to sort based on the natural order for Wiki pages.
// But, sure, you must first tell Swift what this natural order is.
        pages = items.query.pages.values.sorted()

So consider the bigger lesson here. Maybe the goal isn't how to add more code to a structure and bury the sorting code. Perhaps the goal is to learn a new Swift technique.

Protocol Lesson


It seems @twostraws is reinforcing a lesson about adding Protocols to data structures. By implementing Protocols, your structures can benefit from a windfall of new and useful capabilities. Indeed, he's sharing the magic of the Comparable protocol. This protocol requires you to implement one function that returns a boolean when two of your custom objects are compared. Does a fork sort higher than a spoon? Do shirts sort higher than trousers? You decide.

You're in control of how your objects sort.

For Wiki articles, you can implement the protocol and compare the titles of two Wiki articles. You're in charge and you're telling swift the natural sort order for Wiki articles is their titles.

When you implement the Comparable protocol you can sort a list of students, a list of museum sculptures, a list of favorite SF Symbols. The thing is, you get to decide the natural sort order by implementing the Comparable protocol.

Inserting Links


Here's another lesson to consider. Rather than having messages with rather long URLs that span the width of a message, please take a moment to review how to embed a link in the forum messages. You can give each link a brilliant title, whilst hiding the messy URL within the link! Nice! 10-points to Hufflepuff!

See -> Day 72 Lesson on Comparable

1      

Hacking with Swift is sponsored by Essential Developer

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 April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.