Swift Program to Sort the Elements of an Array in Ascending Order


In this article, we will learn how to write a swift program to sort the elements of an array in ascending order.

Here we are going to use the following 2 methods:

  • sort() function without parameter

  • sort(by:) function with parameter

To sort the elements of an array Swift provides an in-built function named sort(). This function can work with or without parameters. By default this function sort the array in ascending order. Otherwise, you can pass < in the by a parameter of the sort(by:) function to sort the array in ascending order.

Method 1: sort() Function Without Parameter

To sort the elements of an array Swift provides an in-built function named sort(). This function can work with or without a parameter but here we use the sort() function without a parameter. By default this function sort the array in ascending order.

Syntax

func sort()

Here the sort() function sorts the array in ascending order.

Algorithm

  • Step 1 − Create an array.

  • Step 2 − Sort the array in ascending order using sort() methods 

Array1.sort()
  • Step 3 − Display the original array.

  • Step 4 − Display the sorted array

Example

In the following example, we did not pass any argument in the sort() function to sort the array in ascending order.

import Foundation
import Glibc

// Creating arrays
var Array1 : [String] = ["birds", "sky", "moon", "cloud"]
var Array2 : [Int] = [23, 56, 7, 5, 84, 2]
var Array3 : [Float] = [3.4, 67.3, 1.2, 5.6, 78.3]

// Sorting the array in ascending order
// Using sort() method
print("Before Sorting Array 1 is:", Array1)
Array1.sort()
print("After Sorting Array 1 is", Array1)

print("\nBefore Sorting Array 2 is:", Array2)
Array2.sort()
print("After Sorting Array 2 is", Array2)

print("\nBefore Sorting Array 3 is:", Array3)
Array3.sort()
print("After Sorting Array 3 is", Array3)

Output

Before Sorting Array 1 is: ["birds", "sky", "moon", "cloud"]
After Sorting Array 1 is ["birds", "cloud", "moon", "sky"]

Before Sorting Array 2 is: [23, 56, 7, 5, 84, 2]
After Sorting Array 2 is [2, 5, 7, 23, 56, 84]
	
Before Sorting Array 3 is: [3.4, 67.3, 1.2, 5.6, 78.3]
After Sorting Array 3 is [1.2, 3.4, 5.6, 67.3, 78.3]

Here in the above code, we create an array of three different types(that are Int, float, and String). Now using the sort() function we sort all the arrays in ascending order(low to high).

Method 2: sort() Function With Parameter

To sort the elements of an array in ascending order we use the pre-defined sort(by:) function with the by parameter. Here the by parameter is responsible for the order of a sorted array. If the value of the by parameter is < then the sort(by:) function sort the array into ascending order. Or if the value of the by parameter is >, then the sort(by:)function sorts the array into descending order.

Syntax

func sort(by: <)

Here we pass < in the sort() function to sort the array in ascending order.

Algorithm

  • Step 1 − Create an array.

  • Step 2 − Sort the array in ascending order using sort(by:) methods 

Array1.sort(by:>)
  • Step 1 − Display the original array.

  • Step 2 − Display the sorted array

Example

In the following example, we use the parameter of the sort(by:) method to sort the array in ascending order.

import Foundation
import Glibc

// Creating arrays
var Array1 : [String] = ["birds", "sky", "moon", "cloud"]
var Array2 : [Int] = [73, 2, 87, 4, 9, 1]
var Array3 : [Float] = [84.3, 2.1, 3.5, 9.3, 1.3]

// Sorting the array in ascending order
// Using sort(by:<) method
print("Before Sorting Array 1 is:", Array1)
Array1.sort(by:<)
print("After Sorting Array 1 is", Array1)

print("\nBefore Sorting Array 2 is:", Array2)
Array2.sort(by:<)
print("After Sorting Array 2 is", Array2)

print("\nBefore Sorting Array 3 is:", Array3)
Array3.sort(by:<)
print("After Sorting Array 3 is", Array3)

Output

Before Sorting Array 1 is: ["birds", "sky", "moon", "cloud"]
After Sorting Array 1 is ["birds", "cloud", "moon", "sky"]

Before Sorting Array 2 is: [73, 2, 87, 4, 9, 1]
After Sorting Array 2 is [1, 2, 4, 9, 73, 87]

Before Sorting Array 3 is: [84.3, 2.1, 3.5, 9.3, 1.3]
After Sorting Array 3 is [1.3, 2.1, 3.5, 9.3, 84.3]

Here in the above code, we create an array of three different types(that are Int, float, and String). Now we pass < in the by parameter of the sort(by:<) function to sort all the arrays in ascending order(low to high).

Conclusion

So this is how we sort the array in ascending order using the same sort() function in two different types that are with and without parameters. The complexity of the sort() function remains the same in both cases O(n log n), where n represents the length of the sequence.

Updated on: 20-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements