struct SearchTag: Codable {
    var cnt: Int?
    var hashtag: String?
}

var taglist: [SearchTag] = [SearchTag(cnt: 3, hashtag: "사과"),
                            SearchTag(cnt: 1, hashtag: "바나나"),
                            SearchTag(cnt: 4, hashtag: "귤"),
                            SearchTag(cnt: 2, hashtag: "메론"),
                            SearchTag(cnt: 6, hashtag: "망고"),
]

let tags = taglist.sorted(by: { $0.cnt ?? 0 < $1.cnt ?? 0 }).map {$0.hashtag ?? ""}

 

[결과]

tags : 바나나, 메론, 사과, 귤, 망고

+ Recent posts