gpt4 book ai didi

根据字符串出现次数快速排序结果集

转载 作者:行者123 更新时间:2023-11-28 15:41:30 26 4
gpt4 key购买 nike

你好我正在尝试在 swift 3.0 中实现搜索栏,它已经部分完成,我没有得到预期的结果,这是我得到的结果是这样的

enter image description here

这是我的 Controller

     class MasterViewController: UITableViewController
{
// MARK: - Properties
var detailViewController: DetailViewController? = nil
var candies = [Candy]()
var filteredCandies = [Candy]()
let searchController = UISearchController(searchResultsController: nil)

// MARK: - Viewcontroller lifecycle
override func viewDidLoad()
{
super.viewDidLoad()
setupSearchVC()
setupModelData()
if let splitViewController = splitViewController {
let controllers = splitViewController.viewControllers
detailViewController = (controllers[controllers.count - 1] as! UINavigationController).topViewController as? DetailViewController
}
}

override func viewWillAppear(_ animated: Bool)
{
clearsSelectionOnViewWillAppear = splitViewController!.isCollapsed
super.viewWillAppear(animated)
}

// MARK: - View setup
func setupSearchVC()
{
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.scopeButtonTitles = ["All", "Chocolate", "Hard", "Other"]
searchController.searchBar.delegate = self
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
}


// MARK: - Segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "showDetail"
{
if let indexPath = tableView.indexPathForSelectedRow
{
let controller = (segue.destination as! UINavigationController).topViewController as! DetailViewController
controller.detailCandy = getDaCorrectCandyNow(row: indexPath.row)
controller.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
controller.navigationItem.leftItemsSupplementBackButton = true
}
}
}

// MARK: - Controller responsibilities
func setupModelData()
{
candies = [
Candy(category:"complete", name:"test"),
Candy(category:"incomplete", name:"test test"),

]
}


func getDaCorrectCandyNow(row: Int) -> Candy
{
let candy: Candy
if searchController.isActive {
candy = filteredCandies[row]
} else {
candy = candies[row]
}
return candy
}

func filterContentForSearchText(_ searchText: String, scope: String = "All")
{
filteredCandies = candies.filter { candy in
let categoryMatch = (scope == "All" ? true : (candy.category == scope))
let searchValueIsNotEmpty = (categoryMatch && candy.name.lowercased().contains(searchText.lowercased()) || candy.category.lowercased().contains(searchText.lowercased()))
let searchValueIsEmpty = (searchText == "")
return searchValueIsEmpty ? categoryMatch : searchValueIsNotEmpty
}
tableView.reloadData()
}
}

这是我的表格

        override func tableView(_ tableView: UITableView, 
numberOfRowsInSection section: Int) -> Int
{
if searchController.isActive {
return filteredCandies.count
}
return candies.count
}



func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath)
let candy: Candy = getDaCorrectCandyNow(row: indexPath.row)
cell.textLabel?.text = candy.name
cell.detailTextLabel?.text = candy.category
return cell
}


// MARK: - Search bar Delegate
func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int)
{
print("*searchBar - ")
filterContentForSearchText(searchBar.text!, scope: searchBar.scopeButtonTitles![selectedScope])
}

// MARK: - Search View Controller Delegate
public func updateSearchResults(for searchController: UISearchController)
{
let searchBar = searchController.searchBar
print("*updateSearchResults - \(searchBar.text)")
let scope = searchBar.scopeButtonTitles![searchBar.selectedScopeButtonIndex]
filterContentForSearchText(searchController.searchBar.text!, scope: scope)
}
}

我希望我的结果显示在没有的基础上。我的结果集中出现的次数例如如果我在搜索栏中搜索“测试”和如果我的结果集是这样的 设置糖果 = [ 糖果(类别:“完整”,名称:“测试”), 糖果(类别:“不完整”,名称:“测试测试”)]我希望 Candy(category:"incomplete", name:"test test") 首先显示,因为它有两个“测试”项目,我该怎么做?抱歉我的英语不好,请帮忙,提前致谢

最佳答案

您需要做的是计算每个糖果的匹配数并在将数据返回到 filteredCandies 数组之前对数据进行排序。为此,您需要修改过滤器功能。

您可以使用 map() 函数来返回包含糖果和匹配计数的元组数组。

从那里您可以过滤匹配项的数量并根据计数对元组数组进行排序。然后您只需从元组中删除计数并仅返回最终数组中的糖果。

要计算匹配的数量,可以使用 String 类型的 components() 函数。使用您的搜索字符串作为 components() 的分隔符将返回比匹配数多一个子字符串(因此 count - 1 将是匹配数)。

这是一个如何使用该方法编写过滤器的示例:

filteredCandies = candies.map 
{
candy in

let matchesCategory = scope == "All" || candy.category == scope
var matchCount = matchesCategory ? 1 : 0

if matchesCategory && searchText != ""
{
let lowerSearch = searchText.lowercased()
matchCount = candy.name.lowercased().components(separatedBy: lowerSearch).count - 1
matchCount += candy.category.lowercased().components(separatedBy: lowerSearch).count - 1
}

return (candy,matchCount)
}
.filter{ $1 > 0 }.sorted{$0.1 > $1.1}.map{$0.0}

关于根据字符串出现次数快速排序结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43644060/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com