Swift Arrays - Sorting Elements

In this article, we will explain how to sort the elements of an Array in Swift.

Sorting with sort()

To sort the elements of a Swift Array, you can use the sort() method.

For example, if you want to sort the elements of an array called names in alphabetical order, you can do the following:

var names = [ "Smith", "Williams", "Miller", "Brown"]
print(names)

names.sort()
print(names)

The output will be as follows. The order of the names array has been sorted alphabetically.

["Smith", "Williams", "Miller", "Brown"]
["Brown", "Miller", "Smith", "Williams"]

If you want to sort in descending order, use sort(by: >), which gives the reverse order of the previous example.

var names = [ "Smith", "Williams", "Miller", "Brown"]
print(names)

names.sort(by: >)
print(names)

The output will be as follows, sorted in descending alphabetical order.

["Smith", "Williams", "Miller", "Brown"]
["Williams", "Smith", "Miller", "Brown"]

Sorting Structs and Class Objects

Next, let's use the sort() method to sort an array of struct objects.


Suppose you have an array called students whose elements are objects of a Student struct, as shown below.

struct Student {
    var name: String
    var age: Int
}

var students = [
    Student(name: "Smith", age: 10),
    Student(name: "Williams", age: 9),
    Student(name: "Miller", age: 8),
    Student(name: "Brown", age: 10),
    Student(name: "Smith", age: 11)
]

for s in students {
    print("Name: \(s.name), Age: \(s.age)")
}
Name: Smith, Age: 10
Name: Williams, Age: 9
Name: Miller, Age: 8
Name: Brown, Age: 10
Name: Smith, Age: 11

To sort this array by name, you can do the following:

struct Student {
    var name: String
    var age: Int
}

var students = [
    Student(name: "Smith", age: 10),
    Student(name: "Williams", age: 9),
    Student(name: "Miller", age: 8),
    Student(name: "Brown", age: 10),
    Student(name: "Smith", age: 11)
]

students.sort(by: {$0.name < $1.name})

for s in students {
    print("Name: \(s.name), Age: \(s.age)")
}

The output will be as follows, showing that the elements are sorted by the student's name in ascending order.

Name: Brown, Age: 10
Name: Miller, Age: 8
Name: Smith, Age: 10
Name: Smith, Age: 11
Name: Williams, Age: 9

If you want descending order, you can write students.sort(by: {$0.name > $1.name}), or swap $0 and $1 like students.sort(by: {$1.name < $0.name}).


If you want to sort by name in ascending order and then by age in descending order, one way is to use a tuple, like this:

struct Student {
    var name: String
    var age: Int
}

var students = [
    Student(name: "Smith", age: 10),
    Student(name: "Williams", age: 9),
    Student(name: "Miller", age: 8),
    Student(name: "Brown", age: 10),
    Student(name: "Smith", age: 11)
]

students.sort(by: {
    ($0.name, $1.age) < ($1.name, $0.age)
})

for s in students {
    print("Name: \(s.name), Age: \(s.age)")
}

The output will be as follows, sorted by name in ascending order and age in descending order.

Name: Brown, Age: 10
Name: Miller, Age: 8
Name: Smith, Age: 11
Name: Smith, Age: 10
Name: Williams, Age: 9

Swift Array - Sorting Elements 1

Getting a Sorted Array with sorted()

The sort() method modifies the order of elements within an Array.

This change is permanent, and once you sort it, the original order is lost.

Sometimes, however, you may want to keep the original Array intact while obtaining a new Array with the elements sorted.

In such cases, you can use the sorted() method, which returns a new Array with the elements sorted.

var names = ["Smith", "Williams", "Miller", "Brown"]

var names_sorted = names.sorted()
names_sorted[0] = "Davis1"

print(names)
print(names_sorted)

The output will be as follows. The names_sorted array contains a copy of the sorted elements, and modifying names_sorted does not affect names.

["Smith", "Williams", "Miller", "Brown"]
["Davis1", "Miller", "Smith", "Williams"]

Reversing the Order with reverse()

If you simply want to reverse the order of elements in a Swift Array, use the reverse() method.

var names = ["Smith", "Williams", "Miller", "Brown"]
print(names)

names.reverse()
print(names)

The output will be as follows, showing the elements in reverse order.

["Smith", "Williams", "Miller", "Brown"]
["Brown", "Miller", "Williams", "Smith"]

Just like sort() and sorted(), reverse() also has reversed(). If you want to create a copy of the Array with the order reversed, you can use reversed().


That's how you can sort the elements of an Array in Swift.