// sorted(by:)
func sorted(
by areInIncreasingOrder: (Self.Element, Self.Element) throws -> Bool
) rethrows -> [Self.Element]
// โญ๏ธ 1. sort a Sequence using `sorted(by:)` method.
// - always returns an Array.
let sorted = todolist.sorted { a, b in a.date < b.date }
import Foundation // Date
// todo item
struct TodoItem {
let date: Date
let item: String
}
// todo item samples
let todos = ["eat", "sleep", "play", "relax"]
let now = Date()
let nextyear = now.yearAfter
extension TodoItem {
static let samples = (1...5).map{ _ in
TodoItem(
date: .random(in: now...nextyear),
item: todos.randomElement()!
)
}
}
let todolist = TodoItem.samples
Standard Lib โฉ Collections โฉ Sequence