gpt4 book ai didi

Swift - 使用多个条件对对象数组进行排序

转载 作者:行者123 更新时间:2023-11-30 11:03:38 24 4
gpt4 key购买 nike

我有一个 Contact 对象数组:

var contacts:[Contact] = [Contact]()

联系类(class):

Class Contact:NSOBject {
var firstName:String!
var lastName:String!
}

我想按 lastName 对该数组进行排序,然后按 firstName 排序,以防某些联系人获得相同的 lastName

我可以按其中一个条件进行排序,但不能同时按两个条件进行排序。

contacts.sortInPlace({$0.lastName < $1.lastName})

如何添加更多条件来对该数组进行排序?

最佳答案

使用元组进行多个条件的比较

按多个条件执行排序的一种非常简单的方法(即通过一个比较排序,如果相等,则通过另一个比较排序)是使用元组,如 <>运算符具有执行字典比较的重载。

/// Returns a Boolean value indicating whether the first tuple is ordered
/// before the second in a lexicographical ordering.
///
/// Given two tuples `(a1, a2, ..., aN)` and `(b1, b2, ..., bN)`, the first
/// tuple is before the second tuple if and only if
/// `a1 < b1` or (`a1 == b1` and
/// `(a2, ..., aN) < (b2, ..., bN)`).
public func < <A : Comparable, B : Comparable>(lhs: (A, B), rhs: (A, B)) -> Bool

例如:

struct Contact {
var firstName: String
var lastName: String
}

var contacts = [
Contact(firstName: "Leonard", lastName: "Charleson"),
Contact(firstName: "Michael", lastName: "Webb"),
Contact(firstName: "Charles", lastName: "Alexson"),
Contact(firstName: "Michael", lastName: "Elexson"),
Contact(firstName: "Alex", lastName: "Elexson"),
]

contacts.sort {
($0.lastName, $0.firstName) <
($1.lastName, $1.firstName)
}

print(contacts)

// [
// Contact(firstName: "Charles", lastName: "Alexson"),
// Contact(firstName: "Leonard", lastName: "Charleson"),
// Contact(firstName: "Alex", lastName: "Elexson"),
// Contact(firstName: "Michael", lastName: "Elexson"),
// Contact(firstName: "Michael", lastName: "Webb")
// ]

这将比较元素的 lastName属性第一。如果它们不相等,则排序顺序将基于 <与他们比较。如果它们相等,那么它将移动到元组中的下一对元素,即比较 firstName属性。

标准库提供<> 2 到 6 个元素的元组的重载。

如果您希望不同的属性具有不同的排序顺序,只需交换元组中的元素即可:

contacts.sort {
($1.lastName, $0.firstName) <
($0.lastName, $1.firstName)
}

// [
// Contact(firstName: "Michael", lastName: "Webb")
// Contact(firstName: "Alex", lastName: "Elexson"),
// Contact(firstName: "Michael", lastName: "Elexson"),
// Contact(firstName: "Leonard", lastName: "Charleson"),
// Contact(firstName: "Charles", lastName: "Alexson"),
// ]

现在将按 lastName 排序降序,然后firstName升序。

<小时/>

定义 sort(by:)需要多个谓词的重载

受到Sorting Collections with map closures and SortDescriptors上的讨论的启发,另一个选项是定义自定义重载 sort(by:)sorted(by:)处理多个谓词——依次考虑每个谓词来决定元素的顺序。

extension MutableCollection where Self : RandomAccessCollection {
mutating func sort(
by firstPredicate: (Element, Element) -> Bool,
_ secondPredicate: (Element, Element) -> Bool,
_ otherPredicates: ((Element, Element) -> Bool)...
) {
sort(by:) { lhs, rhs in
if firstPredicate(lhs, rhs) { return true }
if firstPredicate(rhs, lhs) { return false }
if secondPredicate(lhs, rhs) { return true }
if secondPredicate(rhs, lhs) { return false }
for predicate in otherPredicates {
if predicate(lhs, rhs) { return true }
if predicate(rhs, lhs) { return false }
}
return false
}
}
}

extension Sequence {
func sorted(
by firstPredicate: (Element, Element) -> Bool,
_ secondPredicate: (Element, Element) -> Bool,
_ otherPredicates: ((Element, Element) -> Bool)...
) -> [Element] {
return sorted(by:) { lhs, rhs in
if firstPredicate(lhs, rhs) { return true }
if firstPredicate(rhs, lhs) { return false }
if secondPredicate(lhs, rhs) { return true }
if secondPredicate(rhs, lhs) { return false }
for predicate in otherPredicates {
if predicate(lhs, rhs) { return true }
if predicate(rhs, lhs) { return false }
}
return false
}
}
}

(不幸的是,secondPredicate: 参数是必需的,以避免与现有的 sort(by:) 重载产生歧义)

这让我们可以说(使用之前的 contacts 数组):

contacts.sort(by:
{ $0.lastName > $1.lastName }, // first sort by lastName descending
{ $0.firstName < $1.firstName } // ... then firstName ascending
// ...
)

print(contacts)

// [
// Contact(firstName: "Michael", lastName: "Webb")
// Contact(firstName: "Alex", lastName: "Elexson"),
// Contact(firstName: "Michael", lastName: "Elexson"),
// Contact(firstName: "Leonard", lastName: "Charleson"),
// Contact(firstName: "Charles", lastName: "Alexson"),
// ]

// or with sorted(by:)...
let sortedContacts = contacts.sorted(by:
{ $0.lastName > $1.lastName }, // first sort by lastName descending
{ $0.firstName < $1.firstName } // ... then firstName ascending
// ...
)

尽管调用站点不像元组变体那么简洁,但您可以更加清楚地了解比较的内容和顺序。

<小时/>

符合Comparable

如果您要定期进行此类比较,如 @AMomchilov & @appzYourLife建议,你可以遵守ContactComparable :

extension Contact : Comparable {
static func == (lhs: Contact, rhs: Contact) -> Bool {
return (lhs.firstName, lhs.lastName) ==
(rhs.firstName, rhs.lastName)
}

static func < (lhs: Contact, rhs: Contact) -> Bool {
return (lhs.lastName, lhs.firstName) <
(rhs.lastName, rhs.firstName)
}
}

现在只需调用 sort()对于升序:

contacts.sort()

sort(by: >)对于降序排列:

contacts.sort(by: >)
<小时/>

在嵌套类型中定义自定义排序顺序

如果您想要使用其他排序顺序,您可以在嵌套类型中定义它们:

extension Contact {
enum Comparison {
static let firstLastAscending: (Contact, Contact) -> Bool = {
return ($0.firstName, $0.lastName) <
($1.firstName, $1.lastName)
}
}
}

然后简单地调用:

contacts.sort(by: Contact.Comparison.firstLastAscending)

关于Swift - 使用多个条件对对象数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52992624/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com