Open In App

Sorting an Array in Swift

Last Updated : 02 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Swift support different type of collections and array is one of them. An array is an ordered collection of the same type of values like int, string, float, etc., you are not allowed to store the value of another type in an array(for example, an int type can only store integer values, not string value). It is a generic collection and can store duplicate values of the same type. If an array is assigned to a variable then that array is mutable means you are allowed to change the content and the size of the array. If an array is assigned to a constant then that array is immutable which means you are not allowed to change the content and size of the array. In Swift, we can also sort arrays in ascending and descending order. To sort the array we use the sort() function. This function is used to sort the elements of the array in a specified order either in ascending order or in descending order. It uses the “>” operator to sort the array in descending order and the “<” operator to sort the array in ascending order. By default, this method sorts the array in ascending order. 

Syntax:

arrayName.sort(by: operator)

Here, 

arrayName is the name of the array object

Parameter: This method only takes one parameter which is optional. If we pass less than operator(<) in this method then it will sort the array in ascending order. Whereas if we pass greater than operator (>)in this method then it will sort the array in descending order.

Return Value: This method does not return any value, instead of a value it will change the order of the array. 

Example:

Swift




// Swift program to sort the array
import Swift
  
// Creating an array of Authors Name
// Here the array is of string type
var AuthorName = ["Rohit", "Poonam", "Anu", "Susmita", "Buvan", "Mohit"]
  
print("Author's name before sorting:", AuthorName)
  
// Sorting the AuthorName array
// Using sort() function
AuthorName.sort()
  
// Displaying the final result
print("Author's name after sorting:", AuthorName)
  
// Creating an array of article numbers
// Here the array is of int type
var ArticleNumber = [23, 1, 90, 3, 56, 23, 0, 6, 12]
print("----------------------")
  
print("Article numbers before sorting:", ArticleNumber)
  
// Sorting the ArticleNumber array
// Using sort() function
ArticleNumber.sort()
  
// Displaying the final result
print("Article numbers after sorting:", ArticleNumber)


Output:

Author's name before sorting: ["Rohit", "Poonam", "Anu", "Susmita", "Buvan", "Mohit"]
Author's name after sorting: ["Anu", "Buvan", "Mohit", "Poonam", "Rohit", "Susmita"]
----------------------
Article numbers before sorting: [23, 1, 90, 3, 56, 23, 0, 6, 12]
Article numbers after sorting: [0, 1, 3, 6, 12, 23, 23, 56, 90]

Sorting Array in Ascending Order

We can sort the array in ascending order by passing less than operator (<) in the sort() function. 

Syntax:

arrayName.sort(by: <)

Here, 

arrayName is the name of the array object

Example:

Swift




// Swift program to sort the array in ascending Order
import Swift
  
// Creating an array of Authors Name
// Here the array is of string type
var AuthorName = ["Sumit", "Neha", "Anu", "Susmita", "Buvan", "Govind"]
  
print("Author's name before sorting:", AuthorName)
  
// Sorting the AuthorName array in ascending Order
// By passing less than operator(<) in sort() function
AuthorName.sort(by:<)
  
// Displaying the final result
print("Author's name after sorting:", AuthorName)
  
// Creating an array of article numbers
// Here the array is of int type
var ArticleNumber = [3, 20, 10, 3, 86, 23, 0, 699, 12]
print("----------------------")
  
print("Article numbers before sorting:", ArticleNumber)
  
// Sorting the ArticleNumber array in ascending Order
// By passing less than operator(<) in sort() function
ArticleNumber.sort(by:<)
  
// Displaying the final result
print("Article numbers after sorting:", ArticleNumber)


Output:

Author's name before sorting: ["Sumit", "Neha", "Anu", "Susmita", "Buvan", "Govind"]
Author's name after sorting: ["Anu", "Buvan", "Govind", "Neha", "Sumit", "Susmita"]
----------------------
Article numbers before sorting: [3, 20, 10, 3, 86, 23, 0, 699, 12]
Article numbers after sorting: [0, 3, 3, 10, 12, 20, 23, 86, 699]

Sorting Array in Descending Order

We can sort the array in descending order by passing greater than operator (>) in the sort() function. 

Syntax:

arrayName.sort(by: >)

Here, 

arrayName is the name of the array object

Example:

Swift




// Swift program to sort the array in descending Order
import Swift
  
// Creating an array of Authors Name
// Here the array is of string type
var AuthorName = ["Punit", "Neha", "Amu", "Susmita", "Fiza", "Govind"]
  
print("Author's name before sorting:", AuthorName)
  
// Sorting the AuthorName array in descending Order
// By passing greater than operator(>) in sort() function
AuthorName.sort(by:>)
  
// Displaying the final result
print("Author's name after sorting:", AuthorName)
  
// Creating an array of article numbers
// Here the array is of int type
var ArticleNumber = [3, 20, 10, 3, 86, 23, 0, 699, 12]
print("----------------------")
  
print("Article numbers before sorting:", ArticleNumber)
  
// Sorting the ArticleNumber array in descending Order
// By passing greater than operator(>) in sort() function
ArticleNumber.sort(by:>)
  
// Displaying the final result
print("Article numbers after sorting:", ArticleNumber)


Output:

Author's name before sorting: ["Punit", "Neha", "Amu", "Susmita", "Fiza", "Govind"]
Author's name after sorting: ["Susmita", "Punit", "Neha", "Govind", "Fiza", "Amu"]
----------------------
Article numbers before sorting: [3, 20, 10, 3, 86, 23, 0, 699, 12]
Article numbers after sorting: [699, 86, 23, 20, 12, 10, 3, 3, 0]


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads