Skip to content

Instantly share code, notes, and snippets.

@alobanov alobanov/Array+Sort.swift
Last active Aug 22, 2017

What would you like to do?
Array sorting by predefined sorting ids list
extension Array {
func sortByArray<T, Z: Hashable>(of ids:[Z],
keyType: Z.Type,
idClosure: (_ obj: T) -> (Z)) -> [T] {
var dict = [Z: T]()
guard let arr = self as? [T] else {
return []
for o: T in arr {
let key = idClosure(o)
dict[key] = o
var output = [T]()
for itemID in ids {
if let obj = dict[itemID] {
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.