Swift A-Z 仿通訊錄 排序

數(shù)據(jù)源遵守協(xié)議,將數(shù)據(jù)源傳入,即可排序

import UIKit

//MARK: 排序?qū)ο髆odel
@objc protocol SortObjectModel {
    
    var SortKey: String { get }
}

//MARK: 排序成功處理
typealias successHandler = (_ dataArray : [[SortObjectModel]], _ sectionTitlesArray : [String]) -> Void


extension UILocalizedIndexedCollation {
    
    //MARK: 成功后 --- 得到對(duì)應(yīng)的標(biāo)題文字?jǐn)?shù)組 以及對(duì)應(yīng)分組中數(shù)據(jù)
    static func getCurrentKeysAndObjectsData(needSortArray : [SortObjectModel], finishCallback : @escaping successHandler) -> Void{
        
        // 數(shù)據(jù)源
        var dataArray = [[SortObjectModel]]()
        // 每個(gè)section的標(biāo)題
        var sectionTitleArray = [String]()
    
        let indexedCollation = self.current()
        
        var sortArray = [SortObjectModel]()
        
        for sortObj in needSortArray {
            sortArray.append(sortObj)
        }
        
        // 獲得索引數(shù), 這里是27個(gè)(26個(gè)字母和1個(gè)#)
        let indexCount = indexedCollation.sectionTitles.count
        
        // 每一個(gè)一維數(shù)組可能有多個(gè)數(shù)據(jù)要添加,所以只能先創(chuàng)建一維數(shù)組,到時(shí)直接取來用
        for _ in 0..<indexCount {
            let array = [SortObjectModel]()
            dataArray.append(array)
        }
        
        // 將數(shù)據(jù)進(jìn)行分類,存儲(chǔ)到對(duì)應(yīng)數(shù)組中
        for sortObj in sortArray {
            
            // 根據(jù) SortObjectModel 的 objValue 判斷應(yīng)該放入哪個(gè)數(shù)組里
            // 返回值就是在 indexedCollation.sectionTitles 里對(duì)應(yīng)的下標(biāo)
            let sectionNumber = indexedCollation.section(for: sortObj, collationStringSelector: #selector(getter: SortObjectModel.SortKey))
            
            // 添加到對(duì)應(yīng)一維數(shù)組中
            dataArray[sectionNumber].append(sortObj)
        }
        
        // 對(duì)每個(gè)已經(jīng)分類的一維數(shù)組里的數(shù)據(jù)進(jìn)行排序,如果僅僅只是分類可以不用這步
        for i in 0..<indexCount {
            
            // 排序結(jié)果數(shù)組
            let sortedPersonArray = indexedCollation.sortedArray(from: dataArray[i], collationStringSelector: #selector(getter: SortObjectModel.SortKey))
            // 替換原來數(shù)組
            dataArray[i] = sortedPersonArray as! [SortObjectModel]
        }
        
        // 用來保存沒有數(shù)據(jù)的一維數(shù)組的下標(biāo)
        var tempArray = [Int]()
        
        for (i, array) in dataArray.enumerated() {
            
            if array.count == 0 {
                tempArray.append(i)
            } else {
                // 給標(biāo)題數(shù)組添加數(shù)據(jù)
                sectionTitleArray.append(indexedCollation.sectionTitles[i])
            }
        }
        
        // 刪除沒有數(shù)據(jù)的數(shù)組
        for i in tempArray.reversed() {
            dataArray.remove(at: i)
        }
        
        //將得到新的 分組數(shù)據(jù)以及標(biāo)題數(shù)組 
        finishCallback(dataArray, sectionTitleArray)
        
    }
    
}

調(diào)用:

UILocalizedIndexedCollation.getCurrentKeysAndObjectsData(needSortArray: data) { (models, titles) in
       // models即為按字母排好序的數(shù)組
       // titles: 字母數(shù)組
 }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容