-
-
Save sketchytech/029c00a74a4217a89797 to your computer and use it in GitHub Desktop.
Swift: Sort subsequent arrays based on the Sorter generated by the first
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
typealias Sorter = Array<AnyObject> -> Array<AnyObject> | |
enum SortType { | |
case Ascending | |
case Descending | |
} | |
extension Array where Element:Comparable { | |
func sorter(sortType:SortType = .Ascending) -> Sorter { | |
// turn array into an array of optionals so that we don't get confused over identical values | |
var array:[Element?] = self.map({$0}) | |
let arraySorted = self.sort(sortType == .Ascending ? {$0 < $1} : {$0 > $1}) | |
var dic2 = [Int:Element]() | |
for a in arraySorted.enumerate() { | |
dic2[a.index] = a.element | |
} | |
var dic3 = [Int:Int]() | |
for i in 0..<self.count { | |
let obj = dic2[i] | |
for a in array.enumerate() { | |
if a.element != nil && a.element! == obj! { | |
dic3[a.index] = i | |
array[a.index] = nil | |
} | |
} | |
} | |
let sorter:Sorter = {(array:Array<AnyObject>) in | |
var a = [AnyObject](count:array.count, repeatedValue:0) | |
for i in 0..<dic3.count { | |
if i < array.count { | |
a[dic3[i]!] = array[i] | |
} | |
} | |
return a | |
} | |
return sorter | |
} | |
} | |
func sortArrays<T:Comparable>(arrays:[[AnyObject]], basedOn:[T], sortType:SortType = .Ascending) -> [[AnyObject]] { | |
let sorter = basedOn.sorter(sortType) | |
var sortedArrays = [[AnyObject]]() | |
for arr in arrays { | |
sortedArrays.append(sorter(arr)) | |
} | |
return sortedArrays | |
} |
I've written more or less equivalent code using generics:
https://gist.github.com/callionica/289d33bc6ec1629ffd36
I hope you find it useful.
A few things to note:
- In your implementation, you sort the array and then attempt to reconstruct the sort order of the indexes with some rather complicated code involving dictionaries and optionals. That code is unnecessary if you call enumerate() first then call sort(). If you do that, you'll know exactly how the index order has changed and you won't need to try to reconstruct it by comparing elements.
- Because you have passed your columns in to your function in an array, you're forced to hide the original columns' type information from the compiler. That means that you need to do type conversions on the results of your function. If instead, you pass each column as a separate parameter as I do (or alternatively, you could use tuples), you can preserve the type information so that you don't need to cast. (The downside is that you need to provide overloads of the function up to the number of columns you wish to support, but it's just minor differences from one overload to the next)
- In my implementation, I always sort by the column provided as the first argument. You can adjust to match your function without changing the algorithm - just create an extra parameter for each overload.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let's assume you have some arrays, e.g.
In order to sort all of the arrays based on the sorting pattern of one of them, do the following:
This will return you an array of AnyObject arrays, which must then be cast to the relevant type, e.g.
Note that if you wish the array on which the order is based to also be sorted, this must be within the array of arrays, as you see in the example above.