JKT JKT - 4 months ago 5
Swift Question

The shortest way to create a array in descending order in Swift

I can create an array in ascending order in Swift in the following way

let x = Array(100...999)

How if I do the opposite, e.g something like

let x = Array(999...100) //Does not work


How about this?

let x = Array((100...999).reverse())

P.S. The Stridable protocol is handy for when you arithmetic progressions other than just +1.

Here's an example of using it here: (Swift 2.2)

let x = Array(999.stride(through: 100, by: -1))

And in Swift 3.0:

let x = Array(stride(from: 999, through: 100, by: -1))