Created
April 6, 2017 19:41
-
-
Save ohetzendorfer/d50195bb73d72ab2356be7c81bf86c27 to your computer and use it in GitHub Desktop.
Array sort with enum
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
//: Playground - noun: a place where people can play | |
import UIKit | |
enum Type { | |
case one, two, three | |
var order: Int { | |
switch self { | |
case .one: return 1 | |
case .two: return 2 | |
case .three: return 3 | |
} | |
} | |
} | |
struct Object { | |
var type: Type | |
init(type: Type) { | |
self.type = type | |
} | |
} | |
var objects = [Object]() | |
objects.append(Object(type: .two)) | |
objects.append(Object(type: .three)) | |
objects.append(Object(type: .one)) | |
objects.sort { | |
return $0.type.order < $1.type.order | |
} | |
for object in objects { | |
print("Object: \(object)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment