Skip to content

Commit a7d1dd1

Browse files
committed
Merge pull request #165 from sgl0v/nsmutablearray_sort
Implemented various sorting methods from the NSMutableArray class
2 parents e35f973 + 0cc2416 commit a7d1dd1

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
@@ -730,7 +730,6 @@ public class NSMutableArray : NSArray {
730730
replaceObjectsInRange(NSMakeRange(0, count), withObjectsFromArray: otherArray)
731731
}
732732
}
733-
public func sortUsingFunction(compare: @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Void>) -> Int, context: UnsafeMutablePointer<Void>) { NSUnimplemented() }
734733

735734
public func insertObjects(objects: [AnyObject], atIndexes indexes: NSIndexSet) {
736735
precondition(objects.count == indexes.count)
@@ -760,9 +759,18 @@ public class NSMutableArray : NSArray {
760759
objectIndex += range.length
761760
}
762761
}
763-
764-
public func sortUsingComparator(cmptr: NSComparator) { NSUnimplemented() }
765-
public func sortWithOptions(opts: NSSortOptions, usingComparator cmptr: NSComparator) { NSUnimplemented() }
762+
763+
public func sortUsingFunction(compare: @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Void>) -> Int, context: UnsafeMutablePointer<Void>) {
764+
self.setArray(self.sortedArrayUsingFunction(compare, context: context))
765+
}
766+
767+
public func sortUsingComparator(cmptr: NSComparator) {
768+
self.sortWithOptions([], usingComparator: cmptr)
769+
}
770+
771+
public func sortWithOptions(opts: NSSortOptions, usingComparator cmptr: NSComparator) {
772+
self.setArray(self.sortedArrayWithOptions(opts, usingComparator: cmptr))
773+
}
766774

767775
public convenience init?(contentsOfFile path: String) { NSUnimplemented() }
768776
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)