学做网站好吗,seo网站推广可以自己搞吗,长沙专业建网站公司,网站建设三折页有时候我们需要用两个或者三个参数进行,排序比较。 lexicographicallyprecedes函数介绍 lexicographicallyprecedes可以看下苹果官方文档的介绍。
这里简单说一下:
func lexicographicallyPrecedes<OtherSequence>(_ other: OtherSequence) ->…
有时候我们需要用两个或者三个参数进行,排序比较。
lexicographicallyprecedes函数介绍
lexicographicallyprecedes可以看下苹果官方文档的介绍。
这里简单说一下:
func lexicographicallyPrecedes<OtherSequence>(_ other: OtherSequence) -> Bool where OtherSequence : Sequence, Self.Element == OtherSequence.Element
返回一个布尔值,该值使用小于操作符(<)比较元素,指示该序列是否在词典(字典)排序中的另一个序列之前。
我们看源码可以看出,这个函数的功能,是两个Sequence的比较。具体怎么比较实现方法,应该和我们手动写方法挨个比较一样。
多条件排序
然后我们就可以这样写多条件排序了,我们把多个属性做成序列对比即可。
先看下演示效果
Demo代码:
struct people{var lastName:String?var fristName:String?
}var persons = [people.init(lastName: "c", fristName: "120"),people.init(lastName: "a", fristName: "BBB"),people.init(lastName: "b", fristName: "110"),people.init(lastName: "a", fristName: "AAA")]let new = persons.sorted { p0, p1 inlet left = [p0.lastName,p0.fristName]let right = [p1.lastName,p1.fristName]return left.lexicographicallyPrecedes(right){$0?.localizedCaseInsensitiveCompare($1!) == .orderedAscending}
}