How to Alphabetize an Array of Strings for iOS
This post originally appeared on Ryan Rosello's blog. Read more at Ryan Rosello on Medium. Back in the old days — well, really just last week — I was working on an assignment and needed to alphabetize a mutable array of items inside a model shopping cart. This lead me on an internet journey through […]
This post originally appeared on Ryan Rosello's blog. Read more at Ryan Rosello on Medium.
Back in the old days — well, really just last week — I was working on an assignment and needed to alphabetize a mutable array of items inside a model shopping cart. This lead me on an internet journey through Stackoverflow, Apple documentation, kitten fail videos, and the world’s worst sandwiches, only to end up back again at the same first internet hits on the subject.
I knew I was missing something, and so began a long-winded series of trial and error before surrendering to the idea that brute force would not accomplish the goal of writing working code. I was actually going to have to learn something new. This is a harrowing account of that expedition, the ship’s crew, and its findings.
My first inquiry regarding alphabetizing an array of strings lead me to this solution:
I had no idea what all that meant, so I ate a sandwich. Peanut butter and jelly. Not the world’s worst sandwich, but still not very satisfying. On a full stomach, I decided I wouldn’t stop until I understood exactly how an array of strings is alphabetized! Focusing on the mutable array portion, I knew the sortUsingSelector: method was being called on the mutable array, but knew nothing about selectors nor comparators. I found this about selectors:
So that’s pretty cool! If I use a common method across classes, any modification to the method specific to each class will be implemented. Which is useful when comparing and sorting things. Why? Because sorting numbers in ascending order is different than sorting letters into an ascending order, so the methods used to compare: them will need to be specific to them. Cool. But this didn’t get me that much closer to understanding the whole alphabetization process.
My unwavering determination to learn every in and out and the mechanics was crushed mere minutes later, as I was directed towards the various algorithms used for sorting data. Bubble sort, insertion sort, selection sort, quick sort, merge sort… the list goes on. Defeat… This mountain can not be climbed in a day, nor the process summed up in just one blog post, but I did learn some more about how strings are compared, which is at the heart of this whole alphabetizing process.
The comparator @selected for in the mutable array example below (localizedCaseInsensitiveCompare:) has a lot of pieces to it. Some unseen…
I wanted to compare strings of letters so the regular ‘merican english alphabet would be the basis for the comparison. But in computer land, “A” would be alphabetized before “a”. Here is a nice example using aardvarks:
The comparison may be made using ACSII, I don’t know yet. But the “CaseInsensitive” piece gets added to the comparison which handily remedies that dilemma. Again with the Aardvarks:
Sweet! I can now alphabetize my array of strings (names of items) without worrying about capitalization messing with things, but so much is happening here…and with aardvarks too.
Under the hood of a simple compare: method is a full method compare:options:range:locale: which takes three arguments and returns an NSComparisonResult. Whoa… Here we can see Apple’s documentation.
The NSString compare: argument offers up the word to be ranked against the NSString receiver. Simple enough, but then option: takes a mask?! I can’t even…
Here is a quick example showing the usefulness of an added NSNumericSearch option (why is this called a mask?), but the details and variations of options will have to be explored in another episode.
The range: argument dictates the range of the receiver over which the comparison will be performed.
Lastly, there is the locale argument. Why? Because the world is big and there are many languages. Porque el mundo es grande y hay muchos idiomas. 因為這個世界很大，有很多的語言。Vegna þess að heimurinn er stór og það eru mörg tungumál . You get the point. Thank you google translate. Location affects the criteria for comparison and ultimately any sorting of strings of words or letters. And like any great performer, you must play to your audience.
So what have I/we learned? A lot. Or at least a little about a lot of different things. I learned a little about life and a little about love and even something about aardvarks. (The Aardvark(Orycteropus afer) is the only species in its order). Most importantly, I learned that there is a lot more cool and useful things to learn about coding.
Stay tuned for the next installment of Ryan Goes Down A Rabbit Hole.
Here are some helpful and/or fun links I found along the way. Cheers!
stackoverflow – alphabetizing arrays
Knowledge Stack – algorithms in objective c
iOS Developer Library – NSString compare
learn-co-students – sorting basics
The Jungle Store – aardvarks
One Kind – aardvark
Time – worst sandwich
Short List – worst sandwich
Disclaimer: The information in this blog is current as of January 6, 2016. Current policies, offerings, procedures, and programs may differ.