Reputation: 688
How can I sort an array ascending with zero at the end in swift
let score = [3,0,4,6]
let ranking = score.sorted{ $0 < $1 }
I get :
ranking = [0,3,4,6]
I want :
ranking = [3,4,6,0]
Upvotes: 1
Views: 514
Reputation: 22418
Replace any 0
encountered with a large number like Int.max
in the areInIncreasingOrder
predicate parameter for sorted:
let score = [3, 0, 4, 6]
let ranking = score.sorted{ ($0 == 0 ? Int.max : $0) < ($1 == 0 ? Int.max : $1) }
print(ranking)
Output:
[3, 4, 6, 0]
Upvotes: 2