Open In App

Sorting a Set in Swift

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

Swift supports the generic collection and set is one of them. A set is used to store unordered values of the same type. It means you are not allowed to store different types in the set, e.g. a set is of int type then you can only store values of int type not of string type. A is used set instead of an array if the order of the values is not defined or you want to store unique values. Set doesn’t keep duplicate values, it always keeps unique values. It generally uses a hash table to store the elements and we can also easily sort the elements of the set. To do this we use the sorted() function. This function sorts the elements of the set either in ascending or descending order. By default, this method sorts the elements in ascending order if we do not pass any parameter in the function. You can also use the greater than operator(>) in the sort function to sort the set in the descending order or use the less-than operator(<) in the sort function to sort the set in the ascending order. 

Syntax:

setName.sorted(by: operatorValue)

where, 

setName is the object of the set class.

Parameter: This function takes only one parameter that is operatorValue and it is optional. The value of this parameter is either greater than or less than. If we pass greater than operator(>) in the sort function then this function sort the set in the descending order, or if we pass less-than operator(<) in the sort function then this function sort the set in the ascending order. 

Return Value: This function will return the sorted set.

Example:

Swift




// Swift program to sort the elements of the set
import Swift
  
// Creating an set of Emp Name
// Here the set is of string type
var GfgEmpName: Set = ["Sumit", "Poonam", "Punit", "Bittu", "Mohit"]
  
print("Geeks's employee name before sorting:", GfgEmpName)
  
// Sorting the elements of the GfgEmpName set
// Using sorted() function
var result = GfgEmpName.sorted()
  
// Displaying the final result
print("Geeks's employee name after sorting:", result)


Output:

Geeks's employee name before sorting: ["Sumit", "Poonam", "Mohit", "Bittu", "Punit"]
Geeks's employee name after sorting: ["Bittu", "Mohit", "Poonam", "Punit", "Sumit"]

Sorting the Set in Descending Order

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

Syntax:

setName.sorted(by:>)

Example:

Swift




// Swift program to sort the elements of the set descending order
import Swift
  
// Creating an set of Emp Name
// Here the set is of int type
var GfgEmpId: Set = [102, 104, 106, 101, 109, 103]
  
print("Geeks's employee ID before sorting:", GfgEmpId)
  
// Sorting the elements of the GfgEmpId set
// Using sorted() function
var result = GfgEmpId.sorted(by: >)
  
// Displaying the final result
print("Geeks's employee ID after sorting:", result)


Output:

Geeks's employee ID before sorting: [102, 101, 103, 106, 104, 109]
Geeks's employee ID after sorting: [109, 106, 104, 103, 102, 101]

Sorting the Set in Ascending Order

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

Syntax:

setName.sorted(by: <)

Example:

Swift




// Swift program to sort the elements of the set in ascending order
import Swift
  
// Creating an set of Emp Name
// Here the set is of int type
var GfgEmpId: Set = [102, 104, 106, 101, 109, 103]
  
print("Geeks's employee ID before sorting:", GfgEmpId)
  
// Sorting the elements of the GfgEmpId set
// Using sorted() function
var result = GfgEmpId.sorted(by: <)
  
// Displaying the final result
print("Geeks's employee ID after sorting:", result)


Output:

Geeks's employee ID before sorting: [101, 104, 102, 109, 103, 106]
Geeks's employee ID after sorting: [101, 102, 103, 104, 106, 109]


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

Similar Reads