Realm에서 데이터를 정렬하기 위해서는 Results.sorted(byKeyPath:ascending:)
메서드를 사용하면 된다.
let realm = try! Realm()
// Dog 테이블에 있는 내용을 모두 불러옴
let dogs = realm.objects(Dog.self)
// name의 값을 기준으로 내림차순 정렬한다.
let dogsSorted = dogs.sorted(byKeyPath: "name", ascending: false)
let highPriorityTasks = tasks.filter("priority > 5")
print("High priority tasks: \(highPriorityTasks.count)")
let unassignedTasks = tasks.filter("assignee == nil")
print("Unassigned tasks: \(unassignedTasks.count)")
let aliOrJamiesTasks = tasks.filter("assignee IN {'Ali', 'Jamie'}")
print("Ali or Jamie's tasks: \(aliOrJamiesTasks.count)")
let progressBetween30and60 = tasks.filter("progressMinutes BETWEEN {30, 60}")
print("Tasks with progress between 30 and 60 minutes: \(progressBetween30and60.count)")
let aliComplete = tasks.filter("assignee == 'Ali' AND isComplete == true")
print("Ali's complete tasks: \(aliComplete.count)")
// Use [c] for case-insensitivity.
let startWithE = projects.filter("name BEGINSWITH[c] 'e'")
print("Projects that start with 'e': \(startWithE.count)")
let containIe = projects.filter("name CONTAINS 'ie'")
print("Projects that contain 'ie': \(containIe.count)")
// [d] for diacritic insensitivty: contains 'e', 'E', 'é', etc.
let containElike = projects.filter("name CONTAINS[cd] 'e'")
print("Projects that contain 'e', 'E', 'é', etc.: \(containElike.count)")