Sorting a Set in Swift
Last Updated :
02 Jun, 2022
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
import Swift
var GfgEmpName: Set = [ "Sumit" , "Poonam" , "Punit" , "Bittu" , "Mohit" ]
print ( "Geeks's employee name before sorting:" , GfgEmpName)
var result = GfgEmpName.sorted()
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
import Swift
var GfgEmpId: Set = [102, 104, 106, 101, 109, 103]
print ( "Geeks's employee ID before sorting:" , GfgEmpId)
var result = GfgEmpId.sorted(by: >)
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
import Swift
var GfgEmpId: Set = [102, 104, 106, 101, 109, 103]
print ( "Geeks's employee ID before sorting:" , GfgEmpId)
var result = GfgEmpId.sorted(by: <)
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
Share your thoughts in the comments
Please Login to comment...