In Swift, the
sorted(by:) method is used to sort a set in descending order. The
> operator is used alongside this method. We pass the greater-than operator to this method as a parameter.
Note: In Swift, a set is a sequence of unique elements.
This method takes a parameter,
>, which represents the greater-than operator. With this, we can sort the elements of a set.
It returns a set of elements that are sorted in descending order.
// create a set var workers: Set = ["Alicia", "Bethany", "Diana"] var randomNumbers: Set = [22, 12, 100, 4, 35, 1, 2, 9, 90] // get sets sorted in descending order let descendingWorkers = workers.sorted(by: >) let descendingNumbers = randomNumbers.sorted(by: >) // print sorted sets print(descendingWorkers) print(descendingNumbers)
sorted(by:)method, we sort the sets using the
>operator as a parameter. This way, the sets get sorted in descending order. We then store the results to some variables.
View all Courses