Vyacheslav Vyacheslav - 1 month ago 9
Swift Question

Remove item without swap in Realm

If I use this code:

try! self.realm.write {

let deletedObject = self.storage_array[lbl.tag]



let realm = try! Realm()
lazy var storage_array: Results<Storage> = { self.realm.objects(Storage) }()

The item is removed but at this place the last item is appeared.
That is, before: 1,2,3,4, (remove 2) after: 1,4,3

How can I remove this item without changing the arrangement? That is, I want: 1,3,4.


Objects in a Realm have no inherent order. If you'd like the objects in a Results to be in a specific order you should sort them explicitly using Results.sorted(_:ascending:).