# Swift Program to Sort a Set

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 a set.

Set is a primary collection type in Swift. It is an unordered collection which stores unique values of same data type. You are not allowed to store different type of values in the same set. A set can be mutable or immutable.

## Syntax

Following is the syntax of an array −

var mySet = Set()
Or
var mySet : Set = [value1, value2, value3]


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

## Syntax

Following is the syntax −

setName.sorted(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 pass less than operator then it will sort the elements in ascending order.

Below is a demonstration of the same −

Suppose our given input is −

MySet = [44, 29, 3, 92, 49]


The desired output would be −

Sorted set = [3, 29, 44, 49, 92]


## Algorithm

Following is the algorithm −

• Step 1 − Declare an array.

• Step 2 − Sort the set using sorted() function −

mySet.sorted()

• Step 3 − Print the output

## Example

The following program shows how to sort a set.

import Foundation
import Glibc

// Set of integer type
var mySet : Set = [38, 3, 44, 29, 2, 33, 41, 23]
print("Original set:", mySet)

// Sort the set
// Using sorted() function
var resultSet = mySet.sorted()
print("Sorted set:", resultSet)


## Output

Original set: [3, 33, 2, 23, 44, 29, 41, 38]
Sorted set: [2, 3, 23, 29, 33, 38, 41, 44]


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

## Sorting Set in Ascending order

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

### Syntax

Following is the syntax −

mSet.sorted(by:<)


### Example

The following program shows how to sort a set.

import Foundation
import Glibc

// Set of double type
var mySet = [1.4, 0.9, 3.3, 8.2, 9.9, 2.3]
print("Original set:", mySet)

// Sort the set in ascending order
// Using sorted() function
var resultSet = mySet.sorted(by:<)
print("Sorted set:", resultSet)


### Output

Original set: [1.4, 0.9, 3.3, 8.2, 9.9, 2.3]
Sorted set: [0.9, 1.4, 2.3, 3.3, 8.2, 9.9]


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

var resultSet = mySet.sorted(by:<)


Hence the output is [0.9, 1.4, 2.3, 3.3, 8.2, 9.9].

## Sorting Set in Descending order

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

### Syntax

Following is the syntax −

arrName.sorted(by:<)


### Example

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

import Foundation
import Glibc
// Set of string type
var mySet = ["cold", "summer", "latte", "amrut"]
print("Original Set:", mySet)
// Sort the set in descending order
// Using sort() function
var resultSet = mySet.sorted(by:>)
print("Sorted Set:", resultSet)


### Output

Original Set: ["cold", "summer", "latte", "amrut"]
Sorted Set: ["summer", "latte", "cold", "amrut"]


Here, in the above code, we have a set of string type. Now we sort the set in descending order using sort() function −

var resultSet = mySet.sorted(by:>)


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

Updated on 12-Oct-2022 07:39:06