拆分和排序字符串数组快速3 [英] Split and sort string array swift 3

查看:55
本文介绍了拆分和排序字符串数组快速3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找实现这一目标的最有效方法.假设有一个对象数组,按名称的字母顺序排列:

I'm looking for the most efficient way to go about this. Say there's an array of objects, mapped out in alphabetical order by names:

let objectArray = [AnyObject]()
let abcNameObjectArray = ["Amy", "Bernadette", "Brian", "Chris", "Candice"]

如何根据abcNameObject数组中每个String值的第一个字符(按字母顺序)访问此数组中所有具有"A","B"或"C"的值?我正在尝试卸载数据并根据字母顺序可能存在的朋友数量在UITableView的cellForRowAt方法和numberOfRowsInSection方法中返回特定数量的行. 目标是将数组拆分为一组数组,以填充表格视图(如联系人"应用程序),还应访问给定部分的值.也许使用字典可能会更好地解决这个问题?

How would I access all the values in this array that have "A" , "B", or "C" based on the first character of each String value in abcNameObject array, in alphabetical order? I'm trying to unload data and return a specific number of rows in the UITableView's cellForRowAt method and numberOfRowsInSection method based on the number of friends one might have in alphabetical order. The goal is to split the array into a SET of arrays to populate a table view like the Contacts app and ALSO ACCESS the values for a given section. Perhaps utilizing a dictionary might be better to solve this?

推荐答案

解决方案1:

用代码发布我的评论作为答案.检查是否适合您.您可以使用下面打印的第一个数组中的节(将dict.keys.sorted()存储到某个数组中)并将其用作显示该节中单元格的键.

Posting my comment as answer with code. Check if this will work for you. You can use sections from first array printed below(store dict.keys.sorted() into some array) and use that as key for displaying cells in that section.

let abcNameObjectArray = ["Amy", "Bernadette", "Brian", "Chris", "Candice", ""]

let dict = abcNameObjectArray.reduce([String: [String]]()) { (key, value) -> [String: [String]] in
    var key = key
    if let first = value.characters.first {
        let prefix = String(describing: first).lowercased()
        var array = key[prefix]
        
        if array == nil {
            array = []
        }
        array!.append(value)
        key[prefix] = array!.sorted()
    }
    return key
}

print(dict.keys.sorted())
print(dict)

输出:

["a","b","c"]

["a", "b", "c"]

["b":["Bernadette","Brian"],"a":["Amy"],"c":["Candice","Chris"]]

["b": ["Bernadette", "Brian"], "a": ["Amy"], "c": ["Candice", "Chris"]]

解决方案2:

这是rmaddy建议的使用数组数组的另一种解决方案

Here is one more solution using array of arrays as suggested by rmaddy

let abcNameObjectArray = ["Amy", "Bernadette", "Brian", "Chris", "Candice", ""]
let unicodeScalarA = UnicodeScalar("a")!

var arrayOfArrays = abcNameObjectArray.reduce([[String]]()) { (output, value) -> [[String]] in
    var output = output
    
    if output.count < 26 {
        output = (1..<27).map{ _ in return []}
    }
    
    if let first = value.characters.first {
        let prefix = String(describing: first).lowercased()
        let prefixIndex = Int(UnicodeScalar(prefix)!.value - unicodeScalarA.value)
        var array = output[prefixIndex]
        array.append(value)
        output[prefixIndex] = array.sorted()
    }
    return output
}

arrayOfArrays = arrayOfArrays.filter { $0.count > 0}
print(arrayOfArrays)

输出:

[[["Amy"],["Bernadette","Brian"],["Chris","Candice"]]

[["Amy"], ["Bernadette", "Brian"], ["Chris", "Candice"]]

您可以将节数作为此数组的数目,每个成员将给出该节中所需的单元格数.

You can have number of sections as count of this array and each member will give number of cells required in that section.

这篇关于拆分和排序字符串数组快速3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆