字符串排序的最小字母

Minimum letter for string sorting

问题: 我有字符串:"A", "C", "D", "1", "И" (Russian), "你" (Chinese)。我有字符串 let alwaysFirstString = "?" 当我使用常规 iOS 排序时,我希望 alwaysFirstString 始终排在第一位。 alwaysFirstString 应该是什么而不是“?” ?

为什么我需要那个:

在我的应用程序中,我显示联系人,如 Telegram 应用程序:

我在群组 UITableView 中显示联系人。因此,在核心数据的 DBContact 对象中,我有 sectionInfo 字段。通常此字段 = A、B、C、F(姓氏的第一个字母),但对于某些联系人(我希望他们始终排在第一位)我需要其他符号。所以当我在 FetchResultController 中使用常规 sortDesctriptors 时,这个符号应该总是第一个(对所有语言更好)。

代码如下:

let entityName = kDBContact
let fetchRequest = NSFetchRequest<DBContact>(entityName: entityName)
//            let entityDescription = NSEntityDescription.entity(forEntityName: entityName, in: DatabaseManager.sharedInstance.managedObjectContext)
//            fetchRequest.entity = entityDescription
fetchRequest.fetchBatchSize = 20
// sort
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "sectionInfo", ascending: true), NSSortDescriptor(key: "nameAndSurname", ascending: true)]
// sometimes we have predicate when user tries to search smth
var predicate: NSPredicate?
var formatForPredicate = ""
// filter not to show fake contacts
formatForPredicate += "(isFakeContact == false)"
var argumentArray = [AnyObject]()
// text filter in search bar
if help_needToShowFilteredContacts() {
    if formatForPredicate.length > 0 {
       formatForPredicate += " AND "
    }
    formatForPredicate += "((nameAndSurname CONTAINS[cd] %@) OR (phoneNumbers.numberOnlyDigits CONTAINS[cd] %@))"
    argumentArray.append(resultSearchController.searchBar.text! as AnyObject)
    argumentArray.append(resultSearchController.searchBar.text! as AnyObject)
}
sectionNameKeyPath = "sectionInfo"
// filter to show only favourites
if segmentedControl.selectedSegmentIndex == segmented_favouriteIndex {
    sectionNameKeyPath = nil

    if formatForPredicate.length > 0 {
       formatForPredicate += " AND "
    }
    formatForPredicate += "(isFavourite == true)"
}
if formatForPredicate.length > 0 {
   predicate = NSPredicate(format: formatForPredicate, argumentArray: argumentArray)
}
fetchRequest.predicate = predicate
self.fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest as! NSFetchRequest<NSFetchRequestResult>, managedObjectContext: DatabaseManager.sharedInstance.managedObjectContext, sectionNameKeyPath: sectionNameKeyPath, cacheName: nil)

所以你可以看到 fetchRequest.sortDescriptors = [NSSortDescriptor(key: "sectionInfo", ascending: true), NSSortDescriptor(key: "nameAndSurname", ascending: true)] 。所以在 sectionInfo 中我有这个姓氏的第一个字符,我希望注册的联系人(在 sectionInfo 中有特殊字符)始终是第一个。

我用“”表示最大符号,用“”表示最小符号。稍后在获取的结果控制器委托中我有:

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        if let sections = fetchedResultsController?.sections {
            let currentSection = sections[section]
            let name = currentSection.name
            if "" == name {
                return "#"
            }
            else if " " == name {
                return "Registered contacts"
            }
            return name
        }

        return nil
    }

我在这里找到了最后一个符号 What character to use to put an item at the end of an alphabetic list?