Skip to content

Implemented various sorting methods from the NSMutableArray class #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Foundation/NSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,6 @@ public class NSMutableArray : NSArray {
replaceObjectsInRange(NSMakeRange(0, count), withObjectsFromArray: otherArray)
}
}
public func sortUsingFunction(compare: @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Void>) -> Int, context: UnsafeMutablePointer<Void>) { NSUnimplemented() }

public func insertObjects(objects: [AnyObject], atIndexes indexes: NSIndexSet) {
precondition(objects.count == indexes.count)
Expand Down Expand Up @@ -752,9 +751,18 @@ public class NSMutableArray : NSArray {
objectIndex += range.length
}
}

public func sortUsingComparator(cmptr: NSComparator) { NSUnimplemented() }
public func sortWithOptions(opts: NSSortOptions, usingComparator cmptr: NSComparator) { NSUnimplemented() }

public func sortUsingFunction(compare: @convention(c) (AnyObject, AnyObject, UnsafeMutablePointer<Void>) -> Int, context: UnsafeMutablePointer<Void>) {
self.setArray(self.sortedArrayUsingFunction(compare, context: context))
}

public func sortUsingComparator(cmptr: NSComparator) {
self.sortWithOptions([], usingComparator: cmptr)
}

public func sortWithOptions(opts: NSSortOptions, usingComparator cmptr: NSComparator) {
self.setArray(self.sortedArrayWithOptions(opts, usingComparator: cmptr))
}

public convenience init?(contentsOfFile path: String) { NSUnimplemented() }
public convenience init?(contentsOfURL url: NSURL) { NSUnimplemented() }
Expand Down
46 changes: 46 additions & 0 deletions TestFoundation/TestNSArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class TestNSArray : XCTestCase {
("test_sortedArrayWithOptionsUsingComparator", test_sortedArrayWithOptionsUsingComparator),
("test_arrayReplacement", test_arrayReplacement),
("test_arrayReplaceObjectsInRangeFromRange", test_arrayReplaceObjectsInRangeFromRange),
("test_sortUsingFunction", test_sortUsingFunction),
("test_sortUsingComparator", test_sortUsingComparator)
]
}

Expand Down Expand Up @@ -302,4 +304,48 @@ class TestNSArray : XCTestCase {
let emptyArray = NSArray().sortedArrayWithOptions([]) { _,_ in .OrderedSame }
XCTAssertTrue(emptyArray.isEmpty)
}

func test_sortUsingFunction() {
let inputNumbers = [11, 120, 215, 11, 1, -22, 35, -89, 65]
let mutableInput = inputNumbers.bridge().mutableCopy() as! NSMutableArray
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently fails due to a secondary failure of NSArray's mutable copy not returning the correct value on linux

let expectedNumbers = inputNumbers.sort()

func compare(left: AnyObject, right:AnyObject, context: UnsafeMutablePointer<Void>) -> Int {
let l = (left as! NSNumber).integerValue
let r = (right as! NSNumber).integerValue
return l < r ? -1 : (l > r ? 0 : 1)
}
mutableInput.sortUsingFunction(compare, context: UnsafeMutablePointer<Void>(bitPattern: 0))

XCTAssertEqual(mutableInput.map { ($0 as! NSNumber).integerValue}, expectedNumbers)
}

func test_sortUsingComparator() {
// check behaviour with Array's sort method
let inputNumbers = [11, 120, 215, 11, 1, -22, 35, -89, 65]
let mutableInput = inputNumbers.bridge().mutableCopy() as! NSMutableArray
let expectedNumbers = inputNumbers.sort()

mutableInput.sortUsingComparator { left, right -> NSComparisonResult in
let l = (left as! NSNumber).integerValue
let r = (right as! NSNumber).integerValue
return l < r ? .OrderedAscending : (l > r ? .OrderedSame : .OrderedDescending)
}

XCTAssertEqual(mutableInput.map { ($0 as! NSNumber).integerValue}, expectedNumbers);

// check that it works in the way self.sortWithOptions([], usingComparator: cmptr) does
let inputStrings = ["this", "is", "a", "test", "of", "sort", "with", "strings"]
let mutableStringsInput1 = inputStrings.bridge().mutableCopy() as! NSMutableArray
let mutableStringsInput2 = inputStrings.bridge().mutableCopy() as! NSMutableArray
let comparator: (AnyObject, AnyObject) -> NSComparisonResult = { left, right -> NSComparisonResult in
let l = left as! NSString
let r = right as! NSString
return l.localizedCaseInsensitiveCompare(r.bridge())
}
mutableStringsInput1.sortUsingComparator(comparator)
mutableStringsInput2.sortWithOptions([], usingComparator: comparator)
XCTAssertTrue(mutableStringsInput1.isEqualToArray(mutableStringsInput2.bridge()))
}

}