Это будет работать:
//postive ints
let one = 1
let two = 2
let seven = 7
let ninetyThree = 93
//Negative ints
let minusOne = -1
let minusTwo = -2
let minusSeven = -7
let minusNinetyThree = -93
//Doubles
let onePointSeven = 1.7
let pi = Double.pi
let sqareRootOfTwo = sqrt(2)
let minusTwentyPointThree = -20.3
//Create an array containing a mixture of types
let mixedArray: [Any] = [
one,
sqareRootOfTwo,
minusTwo,
seven,
pi,
minusTwo,
minusSeven,
minusOne,
minusNinetyThree,
two,
ninetyThree,
minusTwentyPointThree,
onePointSeven,
]
//Filter the array to just the positive Ints
let positiveInts = (mixedArray.filter {
guard let int = $0 as? Int else { return false }
return int >= 0
} as! [Int]) //Cast it to type [Int]
.sorted { $0 < $1 } //Sort it into numeric order
print("positiveInts = \(positiveInts)")
//Filter the array to just the negative Ints
let negativeInts = (mixedArray.filter {
guard let int = $0 as? Int else { return false }
return int < 0
} as! [Int]) //Cast it to type [Int]
.sorted { $0 < $1 } //Sort it into numeric order
print("negativeInts = \(negativeInts)")
//Filter the array to just the Doubles
let doubles = (mixedArray.filter {
return $0 is Double
} as! [Double]) //Cast it to type [Double]
.sorted { $0 < $1 } //Sort it into numeric order
print("doubes = \(doubles)")
И вывод:
positiveInts = [1, 2, 7, 93]
negativeInts = [-93, -7, -2, -2, -1]
doubes = [-20.300000000000001, 1.4142135623730951, 1.7, 3.1415926535897931]