I have a swift array which I want to filter, here is the array
let array = [apple,workshops,shopping,sports,parties,pantry,pen]
I want to filt
You can just write
let result = words
.filter { $0.contains(keyword) }
.sorted { ($0.hasPrefix(keyword) ? 0 : 1) < ($1.hasPrefix(keyword) ? 0 : 1) }
let words = ["apple", "workshops", "shopping", "sports", "parties", "pantry", "pen", "cat", "house"]
let keyword = "p"
let result = words
.filter { $0.contains(keyword) }
.sorted { ($0.hasPrefix(keyword) ? 0 : 1) < ($1.hasPrefix(keyword) ? 0 : 1) }
// ["pen", "pantry", "parties", "apple", "workshops", "shopping", "sports"]