Everyone I have a array of following File
object
File{
var displayName: String? // eg detail.txt, abc.doc
var size: Int?
..
}
say, I have a array of this file object
let arrayOfFile = [f1,f2,f3,f4.....f1000];
These file objects are getting display in a UITableView
over a screen. There are a search option, where user can search files...
Now the problem is, when user type something into UISearchBar
I need to sort arrayOfFile
array in such a way that the File
object which contains search string (within name) comes first (all in alphabetic sorted order) and other files which doesn't contains search string within name come at the end.
this is what I have tried so far...
self.dataArray = self.searchDataSource.sorted(by: { ( f1, f2) -> Bool in
if f1.displayName.lowercased().strContains(text) && f2.displayName.lowercased().strContains(text){
if f1.displayName.localizedCaseInsensitiveCompare(f2.displayName) == ComparisonResult.orderedAscending{
return true;
}
else if f1.displayName.localizedCaseInsensitiveCompare(f2.displayName) == ComparisonResult.orderedSame{
return true;
}
else if f1.displayName.localizedCaseInsensitiveCompare(f2.displayName) == ComparisonResult.orderedDescending{
return false;
}
else{
return false;
}
}
else if f1.displayName.lowercased().strContains(text){
return true
}else if f2.displayName.lowercased().strContains(text){
return true
}
else{
return false
}
})