Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

790
Visualizações
How to sort Results in Realm

I am using Realm with Swift. I want to sort "pictures" saved in Realm by PhotoCollectionViewController in the reverse order of date. I don't know what should I do. I would be pleased if you could lend me your wisdom.

Models.swift

import RealmSwift

class Entry: Object {
    @objc dynamic var text = ""
    @objc dynamic var date = Date()
    let pictures = List<Picture>()
    }
class Picture: Object {
    @objc dynamic var fullImageName = ""
    @objc dynamic var thumbnailName = ""
    @objc dynamic var entry : Entry?
    }

PhotoCollectionViewController.swift

import UIKit
import RealmSwift

class PhotoCollectionViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

    var pictures : Results<Picture>?

    override func viewWillAppear(_ animated: Bool) {
        getPictures()
    }

    func getPictures() {
        if let realm = try? Realm() {
            pictures = realm.objects(Picture.self)
            //I want to sort pictures in the reverse order of date
            collectionView?.reloadData()
        }
    }
…
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You should be able to sort it with the following code

realm.objects(Picture.self).sorted(byKeyPath: "date", ascending: false)
over 4 years ago · Santiago Trujillo Relatório

0

Sorry I solved it myself. I changed "pictures" as follows, I was able to do what I expected.

pictures = realm.objects(Picture.self).sorted(byKeyPath: "entry.date", ascending: false)

Thank you for the answers and advice.

over 4 years ago · Santiago Trujillo Relatório

0

If I understood correctly, you want to sort the Entry objects and then flatMap to a bunch of Pictures:

var pictures = realm.objects(Entry.self)
                   .sorted(byKeyPath: "date", ascending: false)
                   .flatMap { $0.pictures }
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda