How to sort an array of numbers in Swift

How to sort an array of numbers in Swift

In this post we will give you information about How to sort an array of numbers in Swift. Hear we will give you detail about How to sort an array of numbers in SwiftAnd how to use it also give you demo for it if it is necessary.

Swift has a built-in sort() method by using that we can sort an array both ascending and descending order.

Sort array in ascending order

var numbers = [3,54,1,5,2];numbers.sort() // sortingprint(numbers)

Output:

[1,2,3,5,54]

Sort array in descending order

To sort an array in descending order we need to pass by: > as an argument to the sort() method.

var numbers = [3,54,1,5,2];numbers.sort(by: >) // sorting descending orderprint(numbers)

Output:

[54,5,3,2,1]

Hope this code and post will helped you for implement How to sort an array of numbers in Swift. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US