@@ -525,20 +525,35 @@ open class NSArray : NSObject, NSCopying, NSMutableCopying, NSSecureCoding, NSCo
525
525
}
526
526
527
527
internal func sortedArray( from range: NSRange , options: NSSortOptions , usingComparator cmptr: ( Any , Any ) -> ComparisonResult ) -> [ Any ] {
528
- // The sort options are not available. We use the Array's sorting algorithm. It is not stable neither concurrent.
529
- guard options. isEmpty else {
530
- NSUnimplemented ( )
531
- }
532
-
533
528
let count = self . count
534
529
if range. length == 0 || count == 0 {
535
530
return [ ]
536
531
}
537
532
538
- let swiftRange = Range ( range) !
539
- return allObjects [ swiftRange] . sorted { lhs, rhs in
540
- return cmptr ( lhs, rhs) == . orderedAscending
533
+ let objects = subarray ( with: range)
534
+
535
+ let indexes = UnsafeMutableBufferPointer< CFIndex> . allocate( capacity: range. length)
536
+ withoutActuallyEscaping ( cmptr) { ( cmptr) in
537
+ CFSortIndexes ( indexes. baseAddress!, range. length, CFOptionFlags ( options. rawValue) ) { ( a, b) -> CFComparisonResult in
538
+ switch cmptr ( objects [ a] , objects [ b] ) {
539
+ case . orderedAscending: return kCFCompareLessThan
540
+ case . orderedDescending: return kCFCompareGreaterThan
541
+ case . orderedSame: return kCFCompareEqualTo
542
+ }
543
+ }
544
+ }
545
+
546
+ let result = Array < Any > ( unsafeUninitializedCapacity: range. length) { ( buffer, initializedCount) in
547
+ var destinationIndex = 0
548
+ for index in indexes {
549
+ buffer. baseAddress? . advanced ( by: destinationIndex) . initialize ( to: objects [ index] )
550
+ destinationIndex += 1
551
+ }
552
+ initializedCount = range. length
541
553
}
554
+
555
+ indexes. deallocate ( )
556
+ return result
542
557
}
543
558
544
559
open func sortedArray( comparator cmptr: ( Any , Any ) -> ComparisonResult ) -> [ Any ] {
@@ -828,7 +843,7 @@ open class NSMutableArray : NSArray {
828
843
if type ( of: self ) === NSMutableArray . self {
829
844
_storage. swapAt ( idx1, idx2)
830
845
} else {
831
- NSUnimplemented ( )
846
+ NSRequiresConcreteImplementation ( )
832
847
}
833
848
}
834
849
@@ -906,7 +921,7 @@ open class NSMutableArray : NSArray {
906
921
_storage. insert ( __SwiftValue. store ( otherArray [ idx] ) , at: idx + range. location)
907
922
}
908
923
} else {
909
- NSUnimplemented ( )
924
+ NSRequiresConcreteImplementation ( )
910
925
}
911
926
}
912
927
0 commit comments