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

Added a sortBy method #12

Merged
merged 1 commit into from
Jun 24, 2014
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
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