Swift: Sort Array by sort descriptors

后端 未结 3 623
长发绾君心
长发绾君心 2020-12-30 03:51

I am using coredata so I need sort descriptors for my entities

For example, a Coordinate-entity has this class func:

class func sortDescriptors() -&g         


        
相关标签:
3条回答
  • 2020-12-30 04:24

    There is no built-in method for this, but you can add them using protocol extension:

    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
            }
        }
    }
    

    But note that this will work only when the array elements are classes, not structures, as NSSortDescriptor compareObject method requires arguments conforming to AnyObject

    0 讨论(0)
  • 2020-12-30 04:30

    Another approach to the nice extensions of Damien could be a multi-cast.

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

    Original source: NSHipster

    0 讨论(0)
  • 2020-12-30 04:43

    Swift 3 Version of @Darniel's answer

    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
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题