Swift Program to sort the Dictionary


This tutorial will discuss how to write swift program to sort a Dictionary.

A dictionary is used to store data in the form of key-value pair in the collation without any defined-order. Here the type of the keys are same as well as the type of the values are also same. Each key is behave like an identifier for the associate value inside the dictionary and each value has a unique key. Swift dictionary is just like the real dictionary. It loop up values according to their identifier.

Syntax

  • Following is the syntax for creating dictionary −

Var mydict = [KeyType: ValueType]()
Or
Var mydict : [KeyType:ValueType] = [key1:value1, key2:value2, key3:value3]

In Dictionary, 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 keys or values of the dictionary either in ascending or descending order. This function will return an array of tuples.

  • Following is the syntax −

dictName.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 keys or values in descending order.

< operator − If we pass less than operator then it will sort the keys or values in ascending order.

Closer − It can also accept a closer which accept condition and return bool value.

Below is a demonstration of the same −

Input

Suppose our given input is −

MyDict = ["year1": 1983, "year2": 1987, "year3": 1999, "year4": 1992]

Output

The desired output would be −

Sorted Dictionary = [(key: "year1", value: 1983), (key: "year2", value: 1987), (key: "year3", value: 1999), (key: "year4", value: 1992)]

Sorting Keys of Dictionary in Ascending order

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

Syntax

Following is the syntax −

var sortedDict = mydict.sorted(by: <)

Example

The following program shows how to sort keys of the dictionary.

import Foundation import Glibc // Creating a dictionary var mydict : [Int:String] = [45: "Mickey", 9:"Soniya", 10:"Mino", 1: "Pihu", 3: "Weee"] print("Original Dictionary: ", mydict) // Sorting the keys of dictionary in ascending order var sortedDict = mydict.sorted(by: <) print("Sorted Dictionary in ascending order:", sortedDict)

Output

Original Dictionary: [10: "Mino", 45: "Mickey", 1: "Pihu", 3: "Weee", 9: "Soniya"]
Sorted Dictionary in ascending order: [(key: 1, value: "Pihu"), (key: 3, value: "Weee"), (key: 9, value: "Soniya"), (key: 10, value: "Mino"), (key: 45, value: “Mickey")]

Here, in the above code, we have a dictionary named mydict. Now we sort the keys of the dictionary in ascending order using sorted() function.

var sortedDict = mydict.sorted(by: <)

So the resultant array of tuples is −

[(key: 1, value: "Pihu"), (key: 3, value: "Weee"), (key: 9, value: "Soniya"), (key: 10, value: "Mino"), (key: 45, value: “Mickey")]

Sorting Keys of Dictionary in Descending order

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

Syntax

Following is the syntax −

var sortedDict = mydict.sorted(by: >)

Example

The following program shows how to sort keys of the dictionary.

import Foundation import Glibc // Creating a dictionary var mydict : [Int:String] = [45: "Mickey", 9:"Soniya", 10:"Mino", 1: "Pihu", 3: "Weee"] print("Original Dictionary: ", mydict) // Sorting the keys of dictionary in descending order var sortedDict = mydict.sorted(by: >) print("Sorted Dictionary in descending order:", sortedDict)

Output

Original Dictionary: [9: "Soniya", 3: "Weee", 45: "Mickey", 10: "Mino", 1: "Pihu"]
Sorted Dictionary in descending order: [(key: 45, value: "Mickey"), (key: 10, value: "Mino"), (key: 9, value: "Soniya"), (key: 3, value: "Weee"), (key: 1, value: "Pihu")]

Here, in the above code, we have a dictionary named mydict. Now we sort the keys of the dictionary in descending order using sorted() function.

var sortedDict = mydict.sorted(by: >)

So the resultant array of tuples is −

[(key: 45, value: "Mickey"), (key: 10, value: "Mino"), (key: 9, value: "Soniya"), (key: 3, value: "Weee"), (key: 1, value: “Pihu”)]

Sorting Values By Passing Closer

To sort the values of the dictionary we pass a closer to the sorted() function. Now this function sorts the values of the dictionary according to the passed closed.

Syntax

Following is the syntax −

var sortedDict = mydict.sorted(by: {$0.value > $1.value})

Example

The following program shows how to sort values of the dictionary.

import Foundation import Glibc // Creating a dictionary var mydict : [Int:String] = [45: "Mickey", 9:"Soniya", 10:"Mino", 1: "Pihu", 3: "Weee"] print("Original Dictionary: ", mydict) // Sorting the values of dictionary in descending order var sortedDict = mydict.sorted(by: {$0.value > $1.value}) print("Sorted Dictionary in descending order:", sortedDict)

Output

Original Dictionary: [45: "Mickey", 9: "Soniya", 10: "Mino", 1: "Pihu", 3: "Weee"]
Sorted Dictionary in descending order: [(key: 3, value: "Weee"), (key: 9, value: "Soniya"), (key: 1, value: "Pihu"), (key: 10, value: "Mino"), (key: 45, value: "Mickey")]

Here, in the above code, we have a dictionary named mydict. Now we sort the values of the dictionary in descending order using sorted() function. So we pass the following closer in the sorted() function −

var sortedDict = mydict.sorted(by: {$0.value > $1.value})

Here, $0.value > $1.value means the first value should be greater than the second value. And $0 and $1 represent the first and second arguments passed in the closer. Hence the resultant array of tuple is −

[(key: 3, value: "Weee"), (key: 9, value: "Soniya"), (key: 1, value: "Pihu"), (key: 10, value: "Mino"), (key: 45, value: “Mickey”)]

Updated on: 20-Oct-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements