How to Sort by Multiple Properties in Swift?

Milan Panchal
Mac O’Clock
Published in
2 min readMar 14, 2021

--

Sorting on collection is very easy for single criteria. Swift already has a built-in function for that called sorted (Its in-place variant, called sort). But, sometimes we need to sort the collection by multiple properties or criteria.

To understand this, let’s create a simple struct for Student with the roll number, name, address, and age property.

In order to sort the student list by address we can use the following code:

Student.students.sorted {
return $0.address < $1.address
}

But what happens when the address value will be the same? That’s where multiple criteria sorting comes into the picture.

What is the meaning of multiple criteria sorting?

Multiple criteria sorting mean, two objects are compared by the first criteria. If those criteria are the same, then ties will be broken by the next criteria, and so on until you get the desired ordering.

Now, we will sort the student list by address and age. It means we first compare the address value and if the address value will be the same, we will sort by age.

Student list sorted by address and age

As you can see, its not complex to perform sorting by two criteria. But what happens if address and age both have the same values? In that case we have to introduce the third property to sort the data.

Of course, we can use the same solution as above by adding one more if condition. But as the number of criteria increases, more nested if condition we have to perform which will cause the pyramid of doom problem.

To solve the pyramid of doom, we can use the swift’s Tuples type which was introduced in Swift 4.

Tuples type are used to group multiple values in a single compound Value. The values in a tuple can be of any type, and do not need to be of same type.

In order to sort the data by multiple criteria, we will create the tuple in the same order we are looking to break the ties. So in our case, we will create tuples of (address, age, rollNumber)

It will compare the first values of the tuples together, and, in the case of a tie, continue to compare the next tuple values.

In the above example we have sorted the student list in ascending for all three proprties (address, age and roll number). However, sometime you want to perform different sorting technique for different property.

For example, We want to sort the student list by address and age in ascending, but rollNumber in descending. In that case, we will interchange the rollNumber property in tuple as below.

Questions?

Please feel free to comment below, if you have any questions.

If you like this article, feel free to share it with your friends and leave me a comment. Also, click on the 👏 clap button below to show how much you like the article.

Thanks for reading! 👨🏼‍💻

You can find me on:

Twitter | LinkedIn | GitHub | Medium | HackerRank | LeetCode | Stack Overflow

--

--

Milan Panchal
Mac O’Clock

iOS Team Lead ǁ Swift ǁ Objective-C ǁ Lifelong Learner ǁ Blogger — Join Medium from the following link: https://medium.com/@milanpanchal24/membership