Skip to content

Instantly share code, notes, and snippets.

@sketchytech
Last active February 4, 2016 23:17
Show Gist options
  • Save sketchytech/029c00a74a4217a89797 to your computer and use it in GitHub Desktop.
Save sketchytech/029c00a74a4217a89797 to your computer and use it in GitHub Desktop.
Swift: Sort subsequent arrays based on the Sorter generated by the first
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
}
@sketchytech
Copy link
Author

Let's assume you have some arrays, e.g.

let col0 = [4,3,1,5]
let col1:[UIColor] = [.redColor(),.blueColor(),.greenColor(),.greenColor()]
let col2 = ["c","b","a", "c"]
let col3 = [10.4,16.8,7.2,1.4]

In order to sort all of the arrays based on the sorting pattern of one of them, do the following:

let sortedArrays = sortArrays([col0, col1, col2, col3], basedOn: col0)

This will return you an array of AnyObject arrays, which must then be cast to the relevant type, e.g.

let col0sorted = sortedArrays[0] as! [Int]
let col1sorted = sortedArrays[1] as! [UIColor]
let col2sorted = sortedArrays[2] as! [String]
let col3sorted = sortedArrays[3] as! [Double]

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.

@callionica
Copy link

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:

  1. 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.
  2. 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)
  3. 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