Sorted

The sorted function returns a new, sorted array of elements from a sequence. The elements are sorted according to the provided closure, which is a function that takes two elements as its input and returns a Boolean value indicating whether the first element should come before the second element in the sorted array. Here is the basic syntax for the sorted function:

Syntax:

let sorted = sequence.sorted(by: areInIncreasingOrder)

The areInIncreasingOrder closure is a function that takes two elements as its input and returns a Boolean value indicating whether the first element should come before the second element in the sorted array. The sorted function uses this closure to determine the order in which the elements should be sorted.

Example 1:

Here is an example of using the sorted function to sort an array of integers in ascending order:

Swift




// Swift program to use sorted() function 
let numbers = [5, 3, 1, 4, 2]
  
// Sorting an array of integers in ascending order
let sortedNumbers = numbers.sorted(by: { $0 < $1 })
  
print(sortedNumbers)


Output:

[1, 2, 3, 4, 5]

In this example, the sorted function sorts the numbers array using the closure { $0 < $1 }, which compares the two elements and returns true if the first element is less than the second element.

You can also use the sorted function to sort elements in descending order. 

Example 2:

In the following example, we are using sorted to sort an array of strings in descending order:

Swift




// Swift program to use sorted() function 
let words = ["apple", "banana", "cherry", "date"]
  
// Sorting an array of integers in descending order
let sortedWords = words.sorted(by: { $0 > $1 })
  
print(sortedWords)


Output:

["date", "cherry", "banana", "apple"]

In this example, the sorted function sorts the words array using the closure { $0 > $1 }, which compares the two elements and returns true if the first element is greater than the second element.

Higher-Order Functions in Swift

Higher-order functions are functions that take other functions as arguments or return functions as their output. These functions are an important aspect of functional programming, which is a programming paradigm that focuses on the use of functions to model computation. Higher-order functions enable developers to abstract common patterns of function applications and make code more concise and reusable.

In Swift, higher-order functions are a powerful tool for manipulating and transforming collections of data. Swift provides several higher-order functions as part of its standard library, including map, filter, reduce, and sorted. In this article, we will explore these functions in detail, along with some examples of how they can be used.

Similar Reads

map

The map function takes closure as its argument and applies it to each element in a sequence, returning a new sequence of transformed elements. Here is the basic syntax for the map function:...

compactMap

...

forEach

...

filter

The compactMap function is similar to the map function, but it filters out any nil values from the resulting sequence. This can be useful when working with options, as it allows you to transform a sequence of optional values into a non-optional sequence while ignoring any nil values....

Sorted

...

reduce

...

Contact Us