我想使用另一种比较方法(如localizedCompare、caseInsensitiveCompare或localizedCaseInsensitiveCompare)对我的Swift结构体数组进行排序。而Swift标准字符串数组排序函数是将所有大写字母排在小写字母前面。以下是我的代码:
import Foundation
struct DataStruct {
struct Item {
let title: String
let number: Int
}
static var items = [
Item(title: "apple", number: 30),
Item(title: "Berry", number: 9),
Item(title: "apple", number: 18)]
}
class DataFunctions {
func sortItemsArrayTitle() {
DataStruct.items.sort { $0.title < $1.title }
}
}
一旦调用上述代码,结果为[Berry, apple, apple]。这是不可接受的。有什么建议吗?