Your First iOS & SwiftUI App: Polishing the App, Episode 38: Challenge: Leaderboard Data Model | Kodeco

Using what you learned about Swift ararys, try integrating leaderboard entries into the app’s data model.


This is a companion discussion topic for the original entry at https://www.kodeco.com/38052670-your-first-ios-swiftui-app-polishing-the-app/lessons/38

For sorting the scores, why don’t you have to first make sure that there are more than one entry? Or does sort() work on a single element even though you compare entry1 and entry2?

According to the sort() documentation, the complexity of the call is O(n log n), where n is the length of the collection. Meaning that a single element should be ok and there would be no point in optimizing with a skip on sort for a count of 1.

I am sure sort() is OK with a count of 1, since it works. And I did look at the documentation for both sort() and sort(by:) and it didn’t say. You say the complexity argument, which is most often used to estimate the work involved sorting a very long list, is the answer. However, order n log n when n = 1 is a complexity of order zero (since log 1 == 0) which is meaningless, isn’t it? I bet if the code were open source the count of the list is the first thing done and it returns the entry if the count is one and the fact that the second argument is nil is inconsequential

The O(n log n) is provided in the documentation link above (feel free to ask Apple Dev Support about it), typically in the case where n=1 no operations are performed, which is the condition you mentioned.