Back to Blog

How to Alphabetize an Array of Strings for iOS

Posted by Flatiron School on January 6, 2016

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:

Blog post image: 1QP1qd7Ov_NJg1RLWvwS-7g.png

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:

Blog post image: 11ypjDX7eqIDvAp1Alob_Hg.png

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…

Blog post image: 1C6bJJ4uh0nTVYYmGUBHsHw.png

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:

Blog post image: 1lel1LWf8jeiDaCRiC17yHg.png

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:

Blog post image: 1C2TfnW0WyOO8dJz-CUeBXg.png

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.

Blog post image: 1ARLezawPikXmJooxxPq0NA.jpeg

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.

Blog post image: 1t7JgcviZVo5ax3d_AxuvfQ.png

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.

Blog post image: 1R5PcxoZh8TOe6Lpe6HUZMQ.png

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!

About Flatiron School

More articles by Flatiron School