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

Swift sorting result set depending upon the no of occurrences of string

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

这是我的控制器

     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)
}
 }

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

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

您可以使用 map() 函数来实现 return 组合糖果和匹配计数的元组数组。

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

要计算匹配的数量,可以使用String类型的components()函数。使用您的搜索字符串作为 components() 的分隔符将 return 比匹配数多一个子串(因此 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{  > 0 }.sorted{[=10=].1 > .1}.map{[=10=].0}