[英]How to sort all data of a single column on realm database in swift?
How the database looks like:数据库的样子:
Here is my data model:这是我的数据 model:
class TodoData: Object { class 待办事项数据:Object {
@objc dynamic var todos: String = String()
@objc dynamic var times: String = String()
@objc dynamic var rows: Bool = Bool()
@objc dynamic var primaryID: Int = Int()
} }
I am adding single row data every time but I need to only sort the primary ID column in ascending order.我每次都添加单行数据,但我只需要按升序对主 ID 列进行排序。
Assign your realm config to a variable.将您的 realm 配置分配给一个变量。 The code below assumes you are using the default local config.
下面的代码假设您使用的是默认的本地配置。
let realm = try! Realm()
Set an optional property in your VC to be of type Results ?.将 VC 中的可选属性设置为Results ? 类型。 *Don't forget to import RealmSwift at the top of the VC.
*不要忘记在 VC 的顶部导入 RealmSwift。
var todos: Results<TodoData>?
Assign in ViewDidLoad在 ViewDidLoad 中分配
todos = realm.objects(TodoData.self).sorted(byKeyPath: "primaryID", ascending: true)
Bonus tip: Realm Results also support NSPredicates for filtering.额外提示:Realm 结果也支持 NSPredicates 进行过滤。 Realm sorting
Realm排序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.