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

Commit

Permalink
Added a sortBy method
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinEberhardt committed Jun 24, 2014
1 parent 7e90e1b commit a727831
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ExSwift/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,19 @@ extension Array {
return result

}

/**
* Sorts the array by the given comparison function
* @param isOrderedBefore
* @return An array that is sorted by the given function
*/
func sortBy (isOrderedBefore: (T, T) -> Bool) -> Array<T> {
var clone = self
clone.unshare()
clone.sort(isOrderedBefore)
return clone
}


/**
* Removes the last element from self and returns it
Expand Down
10 changes: 10 additions & 0 deletions ExSwiftTests/ExSwiftArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ class ExtensionsArrayTests: XCTestCase {
array = [1, 2, 3, 4, 5]
}

func testSortBy () {
var sourceArray = [2,3,6,5]
var sortedArray = sourceArray.sortBy {$0 < $1}

// check that the source array as not been mutated
XCTAssertEqualObjects(sourceArray, [2, 3, 6, 5])
// check that the destination has been sorted
XCTAssertEqualObjects(sortedArray, [2, 3, 5, 6])
}

func testReject () {
var odd = array.reject({
return $0 % 2 == 0
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Name | Signature
**`reduceRight`**|`reduceRight <U>(initial: U, combine: (U, Element) -> U) -> U`
**`implode`**|`implode <C: ExtensibleCollection> (separator: C) -> C?`
**`flatten`**|`flatten <OutType> () -> OutType[]`
**`sortBy`**|`sortBy (isOrderedBefore: (T, T) -> Bool) -> Array<T> `

#### Class Methods ####

Expand Down

0 comments on commit a727831

Please sign in to comment.