Last active
November 20, 2023 13:33
-
-
Save clarkeben/d11cfa2b474302b0e04865c6fac8c3dc to your computer and use it in GitHub Desktop.
sort-array-swift
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
struct Car { | |
var make: String | |
var model: String | |
var year: Int | |
var colours: [String] | |
} | |
var cars = [ | |
Car(make: "Toyota", model: "Corolla", year: 2016, colours: ["Red", "Green", "Pink"]), | |
Car(make: "Ford", model: "Mustang", year: 2020, colours: ["Black", "Yellow"]), | |
Car(make: "Tesla", model: "Model 3", year: 2021, colours: ["Black", "White"]) | |
] | |
var sortedCarsByYear = cars.sorted { | |
$0.year > $1.year //Tesla, Fore, Toyta | |
} |
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
//MARK: - Sort | |
var fruits = ["Orange", "Banana", "Strawberry", "Grapes", "Apple"] | |
fruits.sort() //["Apple", "Banana", "Grapes", "Orange", "Strawberry"] | |
//MARK: - Sorted | |
var numbers = [2, 4, 1, 20, 17, 36] | |
var numbersSorted = numbers.sorted() //[1, 2, 4, 17, 20, 36] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment