Created
August 30, 2014 11:25
-
-
Save benvium/acb5d1cd90e6644096d5 to your computer and use it in GitHub Desktop.
Simple sorting in Swift. Sort array of int, array of string, and by a property in instances of a class
This file contains hidden or 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
| // sort int | |
| var ar = [3,2,5,6] | |
| sort(&ar, <) | |
| // or | |
| ar = ar.sorted(<) | |
| print(ar); | |
| // sort by member in class | |
| class Person { | |
| var name = "" | |
| var age = 0 | |
| init(name: String, age:Int) { | |
| self.name = name | |
| self.age = age | |
| } | |
| } | |
| var people = [ Person(name: "bob", age: 23), Person(name: "alice", age: 56), Person(name: "david", age: 88)] | |
| sort(&people) { $0.name < $1.name } | |
| print(people); | |
| for person in people { | |
| print(person.name); | |
| } | |
finally figured this shit out.
self.zones = nearbyZones.sorted { $0["distanceinfeet"].string!.compare($1["distanceinfeet"].string!, options: .NumericSearch) == NSComparisonResult.OrderedAscending }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do you know how to run the sort by converting the strings to a number?