Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Commit

Permalink
added method and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeleisel committed Feb 2, 2015
1 parent 48f084a commit baeabea
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ExSwift/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1098,9 +1098,19 @@ internal extension Array {
return sorted(isOrderedBefore)
}

/**
Sorts the array by the value returned from the block, in ascending order
:param: block the block to use to sort by
:returns: an array sorted by that block, in ascending order
*/
func sortUsing<U:Comparable>(block: ((T) -> U)) -> [T] {
return self.sortBy({ block($0.0) < block($0.1) })
}

/**
Removes the last element from self and returns it.
:returns: The removed element
*/
mutating func pop () -> Element {
Expand Down
7 changes: 7 additions & 0 deletions ExSwiftTests/ExSwiftArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class ExtensionsArrayTests: XCTestCase {
// check that the destination has been sorted
XCTAssertEqual(sortedArray, [2, 3, 5, 6])
}

func testSortUsing() {
let sourceArray: [Int] = [3, 2, 7]
XCTAssertEqual(sourceArray.sortUsing({ $0 / 3 }), [2, 3, 7])
XCTAssertEqual(sourceArray.sortUsing({ $0 % 3 }), [3, 7, 2])
XCTAssertEqual(sourceArray.sortUsing({ -$0 }), [7, 3, 2])
}

func testReject () {
var odd = array.reject({
Expand Down

0 comments on commit baeabea

Please sign in to comment.