In Swift, the sorted()
method is used to sort a set in <
, is used with it as a parameter.
sorted()
set.sorted()
This method doesn't take any parameter.
It returns a set with its elements sorted in ascending order.
<
set.sorted(by: <)
<
: With this less-than operator, we can sort the elements of a set in ascending order. This is possible with the help of the sorted(by:)
method.
It returns the same value as the sorted()
method.
// create some setsvar workers: Set = ["Evangeline", "CHiwendu", "Amara", "Bethany", "Diana"]var randomNumbers: Set = [12, 1, 45, 6, 90, 102, 88]// sort them using the sorted() and the < operatorlet sortedWorkers = workers.sorted()let sortedNumbers = randomNumbers.sorted(by: <)// print sorted setsprint(sortedWorkers)print(sortedNumbers)
workers
set using the sorted()
method.randomNumbers
, using the sorted(by:)
method. Next, we supply the less-than operator <
as a parameter to this method. Finally, we save the sorted result into another variable.