Arrays Resort Swift数组-将名称为空的对象移动到数组末尾

Arrays Resort Swift数组-将名称为空的对象移动到数组末尾,arrays,swift,sorting,Arrays,Swift,Sorting,我有一个数组,我正在排序如下: contactsArray = unified.sorted{$0.name.localizedCaseInsensitiveCompare($1.name) == ComparisonResult.orderedAscending} 问题是我希望将带有空字符串ex“的对象移动到数组的末尾。在这里寻找最有效的解决方案。尝试以下方法: contactsArray = unified.sorted { (a, b) -> Bool in if a.na

我有一个数组,我正在排序如下:

contactsArray = unified.sorted{$0.name.localizedCaseInsensitiveCompare($1.name) == ComparisonResult.orderedAscending}
问题是我希望将带有空字符串ex
的对象移动到数组的末尾。在这里寻找最有效的解决方案。

尝试以下方法:

contactsArray = unified.sorted { (a, b) -> Bool in
    if a.name.isEmpty {
        return false
    } else if b.name.isEmpty {
        return true
    } else {
        return a.name.localizedCaseInsensitiveCompare(b.name) == .orderedAscending
    }
}