Sorting an Array in Swift
By: Steven Curtis
Tags: Programming, Swift, Swift Programming

Sort it! The Integer Example

There are so many reasons why you might want to sort an Array . The usual way we think of, and define sort is by taking a group of elements (a collection in the language of Swift) and change the order of those elements.

So if we take just four elements in an array (we shall call this arr) and have the elements in no particular order we can visualise this as the following diagram:

We can of course set up this array in Swift without too much of a problem. In Swift arrays have an intrinsic order, so the following is a good representation of the array above. Note that we make it a variable so the elements are mutable.

var arr = [4,2,1,3]

Now this isn’t in a sorted order; if we want to make the array be in ascending order.

so to be able to do this we are going to use the sort() function, that sorts the elements in place (that is, changes the order of the elements in arr).

arr.sort()

if we want to create a new array we can similarly use sorted(). If we wanted to store the resultant array into a constant array called sortedArr the following statement would do just that.

let sortedArr = arr.sorted()

So what happened here?

In order to (kind of) automatically sort the array , Swift needed to know how to compare the elements in the array . This is the function of Swift’s comparable protocol, and since Integer conforms to the protocol we didn’t need to do any extra work.

The sorting algorithm used here is not guaranteed to be stable, and you can read more about stable sorts HERE.

What about a descending sort?

We are looking for a resultant arr that looks like the following:

Descending: Verbose

The verbose version of the sort involves a separate function to sort our elements. Now I have used the in-place sort() here, but to use sorted() to create a new array is simply a case of replacing sort() with sorted(), and creating a new array to store the result in (not shown).

/// The SortingAlgorithm

///

/// - Parameters:

/// - first: The First Element to be compared

/// - second: The Second Element to be compared

/// - Returns: A Boolean returning true if the first value should be before the second in the resultant array func sortingAlgorithm(first: Int, second: Int) -> Bool{

return first > second

}

This can then be called using the following:

arr.sort(by: sortingAlgorithm)

Descending: Closure

We can use a closure to sort the array in descending order.

arr.sorted(by: {first,second in

return first > second

})

This is pretty much the same as before, but we have put the separate function kind of “inside” the calling closure (this is an inline closure). That’s pretty good.

Descending: Shorthand argument names

We can see that inline closures can make use of shorthand arguments. That is, the names of the first element, “first” and the second element “second” have been replaced by kind of placeholder names which are $0 and $1.

arr.sorted{

return $0 > $1

}

From this point forwards I’ll be using these shorthand argument names — but with one final change- that is you don’t need to tell Swift to return anything as the closure makes this assumption for you!

arr.sorted{$0 > $1}

Now there is one more step we can take, and that is to not include any argument names at all (that is in the format arr.sort(by: >) ) but I find that a little hard to read, so choose to leave that here.

The same sorting would work for the basic types in Swift, but what about more complex user-created types?