A sorted array type for Swift 4.0+.
SortedArray type, an array that keeps its elements sorted according to a given sort predicate.
Written by Ole Begemann, February 2017.
The current release supports Swift 4.0 and up.
If you need support for older Swift version, here's a list of the latest releases that support specific Swift versions:
Since the code has no dependencies other than the Swift standard library (it doesn't even use Foundation), it should work on all platforms where Swift is available.
I tested it on macOS, iOS, tvOS, and Linux.
Swift Package Manager
Add this to your
// Package.swift import PackageDescription let package = Package( name: "<Your package name>", dependencies: [ .Package(url: "https://github.com/ole/SortedArray.git", majorVersion: 0) ] )
Add this to your
github "ole/SortedArray" ~> 0.7
Integration via Carthage should work for macOS, iOS, tvOS, and watchOS targets.
Clone the repository and add or copy
SortedArray.swift to your project. It has no dependencies.
Help us keep the lights on
0.7.0 - Jun 18, 2018
Drop Swift 3.x support. Please continue to use version 0.6.0 if you need Swift 3 compatibility. The lowest supported version is now Swift 4.0.
Compatibility with Swift 4.2.
SortedArraynow conditionally conforms to
Equatable(on Swift 4.1 and up) and
Hashable(on Swift 4.2 and up) if its
Hashable. Note that the comparator function is not considered for determining equality.
SortedArray.firstIndex(of:)as an alias for
index(of:)(following the standard library). The old method isn’t deprecated yet, but may be in a future release.
The binary search is now implemented using recursion, which (surprisingly to me) turned out to be faster (#21). Thanks @kareman.
General cleanup because we no longer need to support Swift 3. Thanks @klaaspieter.
The performance tests are now in a separate target and do no longer run by default when you hit Cmd+U in Xcode. This speeds up the test suite. On the command line, use
swift test --parallel --filter UnitTeststo only run the unit tests.
0.6.0 - Jan 10, 2018
index(of:)when searching for duplicate elements. Previously,
index(of:)was not guaranteed to return the index of the first matching element when the array contains duplicates. This is now fixed (#16). Thanks @Mamonaku!
anyIndex(of:)(when it’s not important which of multiple matching elements is found) methods to complement
0.5.1 - Aug 8, 2017
Switch back to legacy Xcode build system (required for Carthage compatibility) (#13). Thanks @klaaspieter!
0.5.0 - Jul 16, 2017
- Compatible with Swift 3.2 and Swift 4.0.
- Dropped support for Swift 3.0.
- The methods for removing elements from the array now have the
@discardableResultattribute, avoiding compiler warnings if you ignore the return value. Thanks @DivineDominion!
0.4.0 - Apr 28, 2017
Added equality operators
SortedArray (#9). Thanks @klaaspieter!