Skip to content

Commit 0cc2416

Browse files
committed
Implemented various sorting methods for the NSMutableArray class.
1 parent d9817a4 commit 0cc2416

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

Foundation/NSArray.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,6 @@ public class NSMutableArray : NSArray {
722722
replaceObjectsInRange(NSMakeRange(0, count), withObjectsFromArray: otherArray)
723723
}
724724
}
725-
public func sortUsingFunction(compare: @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Void>) -> Int, context: UnsafeMutablePointer<Void>) { NSUnimplemented() }
726725

727726
public func insertObjects(objects: [AnyObject], atIndexes indexes: NSIndexSet) {
728727
precondition(objects.count == indexes.count)
@@ -752,9 +751,18 @@ public class NSMutableArray : NSArray {
752751
objectIndex += range.length
753752
}
754753
}
755-
756-
public func sortUsingComparator(cmptr: NSComparator) { NSUnimplemented() }
757-
public func sortWithOptions(opts: NSSortOptions, usingComparator cmptr: NSComparator) { NSUnimplemented() }
754+
755+
public func sortUsingFunction(compare: @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Void>) -> Int, context: UnsafeMutablePointer<Void>) {
756+
self.setArray(self.sortedArrayUsingFunction(compare, context: context))
757+
}
758+
759+
public func sortUsingComparator(cmptr: NSComparator) {
760+
self.sortWithOptions([], usingComparator: cmptr)
761+
}
762+
763+
public func sortWithOptions(opts: NSSortOptions, usingComparator cmptr: NSComparator) {
764+
self.setArray(self.sortedArrayWithOptions(opts, usingComparator: cmptr))
765+
}
758766

759767
public convenience init?(contentsOfFile path: String) { NSUnimplemented() }
760768
public convenience init?(contentsOfURL url: NSURL) { NSUnimplemented() }

TestFoundation/TestNSArray.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class TestNSArray : XCTestCase {
3636
("test_sortedArrayWithOptionsUsingComparator", test_sortedArrayWithOptionsUsingComparator),
3737
("test_arrayReplacement", test_arrayReplacement),
3838
("test_arrayReplaceObjectsInRangeFromRange", test_arrayReplaceObjectsInRangeFromRange),
39+
("test_sortUsingFunction", test_sortUsingFunction),
40+
("test_sortUsingComparator", test_sortUsingComparator)
3941
]
4042
}
4143

@@ -302,4 +304,48 @@ class TestNSArray : XCTestCase {
302304
let emptyArray = NSArray().sortedArrayWithOptions([]) { _,_ in .OrderedSame }
303305
XCTAssertTrue(emptyArray.isEmpty)
304306
}
307+
308+
func test_sortUsingFunction() {
309+
let inputNumbers = [11, 120, 215, 11, 1, -22, 35, -89, 65]
310+
let mutableInput = inputNumbers.bridge().mutableCopy() as! NSMutableArray
311+
let expectedNumbers = inputNumbers.sort()
312+
313+
func compare(left: AnyObject, right:AnyObject, context: UnsafeMutablePointer<Void>) -> Int {
314+
let l = (left as! NSNumber).integerValue
315+
let r = (right as! NSNumber).integerValue
316+
return l < r ? -1 : (l > r ? 0 : 1)
317+
}
318+
mutableInput.sortUsingFunction(compare, context: UnsafeMutablePointer<Void>(bitPattern: 0))
319+
320+
XCTAssertEqual(mutableInput.map { ($0 as! NSNumber).integerValue}, expectedNumbers)
321+
}
322+
323+
func test_sortUsingComparator() {
324+
// check behaviour with Array's sort method
325+
let inputNumbers = [11, 120, 215, 11, 1, -22, 35, -89, 65]
326+
let mutableInput = inputNumbers.bridge().mutableCopy() as! NSMutableArray
327+
let expectedNumbers = inputNumbers.sort()
328+
329+
mutableInput.sortUsingComparator { left, right -> NSComparisonResult in
330+
let l = (left as! NSNumber).integerValue
331+
let r = (right as! NSNumber).integerValue
332+
return l < r ? .OrderedAscending : (l > r ? .OrderedSame : .OrderedDescending)
333+
}
334+
335+
XCTAssertEqual(mutableInput.map { ($0 as! NSNumber).integerValue}, expectedNumbers);
336+
337+
// check that it works in the way self.sortWithOptions([], usingComparator: cmptr) does
338+
let inputStrings = ["this", "is", "a", "test", "of", "sort", "with", "strings"]
339+
let mutableStringsInput1 = inputStrings.bridge().mutableCopy() as! NSMutableArray
340+
let mutableStringsInput2 = inputStrings.bridge().mutableCopy() as! NSMutableArray
341+
let comparator: (AnyObject, AnyObject) -> NSComparisonResult = { left, right -> NSComparisonResult in
342+
let l = left as! NSString
343+
let r = right as! NSString
344+
return l.localizedCaseInsensitiveCompare(r.bridge())
345+
}
346+
mutableStringsInput1.sortUsingComparator(comparator)
347+
mutableStringsInput2.sortWithOptions([], usingComparator: comparator)
348+
XCTAssertTrue(mutableStringsInput1.isEqualToArray(mutableStringsInput2.bridge()))
349+
}
350+
305351
}

0 commit comments

Comments
 (0)