Swift program to sort an array

SwiftServer Side ProgrammingProgramming

Swift 4 Online Training

38 Lectures 1 hours

Swift Basics: Learn to Code from Scratch [For Beginners]

13 Lectures 2 hours

How to create Tic-Tac-Toe Game for iPhone using Swift in Xcode - iOS App

7 Lectures 1 hours

This tutorial will discuss how to write swift program to sort an array.

An array is an ordered collection which is used to store same type of data. For example, if any array is of intege r type then it will only store integers, you are strictly not allowed to store elements of other data types like string, float, etc. It can be mutable or immutable.

Syntax

• Following is the syntax of an array −

var myArray = [Type]()
Or
var myArray = [value1, value2, value3]


In Array, we can sort the elements either in ascending or descending order with the help of in built function named sort(). This function sort the elements of array in either ascending or descending order. By default this function sort elements in ascending order.

• Following is the syntax −

arrName.sort(by:operator)


This function takes only one parameter and it is optional −

> operator − If we pass greater than operator then it will sort the elements in descending order.

< operator − If we p ass less than operator then it will sort the elements in ascending order.

Below is a demonstration of the same −

Input

Suppose our given input is −

Myarr = [ 98, 46, 23, 12, 9, 34]


Output

The desired output would be −

Sorted array = [ 9,12, 23, 34, 46, 98]


Algorithm

Following is the algorithm −

• Step 1− Declare an array.

• Step 2− Sort the array using sort() function −

myArr.sort()

• Step 3− Print the output.

Example

The following program shows how to sort an array.

import Foundation
import Glibc

// Array of integer type
var myArr = [34, 87, 2, 39, 3, 1, 39, 4]

print("Original array:", myArr)

// Sort the array
// Using sort() function
myArr.sort()

print("Sorted array:", myArr)


Output

Original array: [34, 87, 2, 39, 3, 1, 39, 4]
Sorted array: [1, 2, 3, 4, 34, 39, 39, 87]


Here, in the above code, we have an array of integer type. Now we sort the array using sort() function. Here this function sort the array in the ascending order.

Sorting array in Ascending order

In Swift, we can sort the array in ascending or der using sort() function. In this function, we set the value of optional parameter to < operator.

Syntax

Following is the syntax −

arrName.sort(by:<)


Example

The following program shows how to sort an array in ascending order.

import Foundation
import Glibc

// Array of double type
var myArr = [3.4, 0.9, 33.3, 87.2, 1.9, 4.3]

print("Original array:", myArr)

// Sort the array in ascending order
// Using sort() function
myArr.sort(by:<)

print("Sorted array:", myArr)


Output

Original array: [3.4, 0.9, 33.3, 87.2, 1.9, 4.3]
Sorted array: [0.9, 1.9, 3.4, 4.3, 33.3, 87.2]


Here, in the above code, we have an array of double type. Now we sort the array in ascending order using sort() function −

myArr.sort(by:<)


Hence the output is [0.9, 1.9, 3.4, 4.3, 33.3, 87.2].

Sorting array in Descending order

In Swift, we can sort the array in descending order using sort() function. In this function, we set the value of optional parameter to operator.

Syntax

Following is the syntax −

arrName.sort(by:<)


Example

The following program shows how to sort an array in descending order.

import Foundation
import Glibc

// Array of string type
var myArr = ["cold", "summer", "latte", "amrut"]

print("Original array:", myArr)

// Sort the array in descending order
// Using sort() function
myArr.sort(by:>)

print("Sorted array:", myArr)


Output

Original array: ["cold", "summer", "latte", "amrut"]
Sorted array: ["summer", "latte", "cold", “amrut"]


Here, in the above code, we have an array of string type. Now we sort the array in desc ending order using sort() function −

arrName.sort(by:>)


Hence the output is ["summer" "latte" "amrut"].

Updated on 10-Oct-2022 12:53:42