【问题标题】:Swift: Sort Array by sort descriptorsSwift:按排序描述符对数组进行排序
【发布时间】:2014-11-12 09:19:36
【问题描述】:

我正在使用 coredata,所以我需要我的实体的排序描述符

例如,坐标实体有这个类函数:

class func sortDescriptors() -> Array<NSSortDescriptor>
{
    return [NSSortDescriptor(key: "sequence", ascending: true)]
}

我在像这样对 CoreData 进行获取请求时使用它:

var request = NSFetchRequest(entityName: entityName)

request.sortDescriptors = T.sortDescriptors()

但是,当我有一个坐标数组作为另一个 coredata 对象的属性时,这是一个 NSSet(即未排序)

为了解决这个问题,我返回如下坐标:

return NSArray(array: coordinates!).sortedArrayUsingDescriptors(Coordinate.sortDescriptors()) as? Array<Coordinate>

感觉很难看,使用 NSArray 只是为了获得sortedArrayUsingDescriptors-方法。是否有类似的方法可以直接在 Swift 数组上执行此操作,即Array&lt;Coordinate&gt; 使用排序描述符?

谢谢!

【问题讨论】:

  • 为什么不用NSSetsortedArrayUsingDescriptors()方法?
  • 我也可以这样做!但问题仍然存在;排序描述符是否与 Swift 数组兼容?
  • @rintaro NSSet 实例根据定义是无序的。我假设您指的是NSArray

标签: ios arrays sorting core-data swift


【解决方案1】:

另一个不错的 Damien 扩展方法可能是多播。

myArray = (myArray as NSArray).sortedArrayUsingDescriptors(tableView.sortDescriptors) as! Array

原文来源:NSHipster

【讨论】:

    【解决方案2】:

    对此没有内置方法,但您可以使用协议扩展添加它们:

    extension MutableCollectionType where Index : RandomAccessIndexType, Generator.Element : AnyObject {
        /// Sort `self` in-place using criteria stored in a NSSortDescriptors array
        public mutating func sortInPlace(sortDescriptors theSortDescs: [NSSortDescriptor]) {
            sortInPlace {
                for sortDesc in theSortDescs {
                    switch sortDesc.compareObject($0, toObject: $1) {
                    case .OrderedAscending: return true
                    case .OrderedDescending: return false
                    case .OrderedSame: continue
                    }
                }
                return false
            }
        }
    }
    
    extension SequenceType where Generator.Element : AnyObject {
        /// Return an `Array` containing the sorted elements of `source`
        /// using criteria stored in a NSSortDescriptors array.
        @warn_unused_result
        public func sort(sortDescriptors theSortDescs: [NSSortDescriptor]) -> [Self.Generator.Element] {
            return sort {
                for sortDesc in theSortDescs {
                    switch sortDesc.compareObject($0, toObject: $1) {
                    case .OrderedAscending: return true
                    case .OrderedDescending: return false
                    case .OrderedSame: continue
                    }
                }
                return false
            }
        }
    }
    

    但请注意,这仅在数组元素是类而不是结构时才有效,因为 NSSortDescriptor compareObject 方法需要符合 AnyObject 的参数

    【讨论】:

    • 向你致敬!这对我来说是一个令人头疼的问题。感谢您帮助我。
    【解决方案3】:

    @Darniel 回答的 Swift 3 版本

    extension MutableCollection where Self : RandomAccessCollection {
        /// Sort `self` in-place using criteria stored in a NSSortDescriptors array
        public mutating func sort(sortDescriptors theSortDescs: [NSSortDescriptor]) {
            sort { by:
                for sortDesc in theSortDescs {
                    switch sortDesc.compare($0, to: $1) {
                    case .orderedAscending: return true
                    case .orderedDescending: return false
                    case .orderedSame: continue
                    }
                }
                return false
            }
    
        }
    }
    
    extension Sequence where Iterator.Element : AnyObject {
        /// Return an `Array` containing the sorted elements of `source`
        /// using criteria stored in a NSSortDescriptors array.
    
        public func sorted(sortDescriptors theSortDescs: [NSSortDescriptor]) -> [Self.Iterator.Element] {
            return sorted {
                for sortDesc in theSortDescs {
                    switch sortDesc.compare($0, to: $1) {
                    case .orderedAscending: return true
                    case .orderedDescending: return false
                    case .orderedSame: continue
                    }
                }
                return false
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-23
      相关资源
      最近更新 更多