11.1. Increasing order

We can call sorted() function to sort an array with the increasing order of it's values.

var arrayOfInt:[Int] = [1,56,89,23,4,6]

print(arrayOfInt.sorted())

// prints: [1, 4, 6, 23, 56, 89]

Let's try to sort an array of Strings.

//array of Strings ...

var arrayOfStrings = ["apple", "orange", "pineapple", "a", "b"]

print(arrayOfStrings.sorted())

//prints: ["a", "apple", "b", "orange", "pineapple"]

The above code sorted the array in a lexicographical order (like how the words in a dictionary are sorted).

Last updated

Was this helpful?