【发布时间】:2017-02-21 01:25:27
【问题描述】:
我有两个数组:
var filteredTitles = [String]()
var filteredTypes = [String]()
我过滤第一个数组作为使用搜索栏的一部分。元素的顺序可能会完全改变。但是,我无法像第一个数组一样过滤第二个数组,因为我不想在搜索时将其计算在内。但我希望第二个数组与第一个数组的顺序相同。所以,回顾一下。如何通过索引过滤一个数组以完美匹配另一个数组?
一个例子:
var filteredArray = ["One", "Two", "Three"]
//Sort the below array to ["1", "2", "3"], the order of the upper array
var toBeFilteredArray = ["2", "1", "3"]
不使用字母或数字顺序,因为在这种情况下不会这样做。
编辑: 致拉塞尔: 如何像这样对标题进行排序:
// When there is no text, filteredData is the same as the original data
// When user has entered text into the search box
// Use the filter method to iterate over all items in the data array
// For each item, return true if the item should be included and false if the
// item should NOT be included
searchActive = true
filteredData = searchText.isEmpty ? original : original.filter({(dataString: String) -> Bool in
// If dataItem matches the searchText, return true to include it
return dataString.range(of: searchText, options: .caseInsensitive) != nil
})
【问题讨论】: