Uploaded image for project: 'Realm Cocoa SDK'
  1. Realm Cocoa SDK
  2. RCOCOA-1201

The result of sort with multiple fields will not be consistent

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • Q1-24FY
    • Affects Version/s: None
    • Component/s: None

      Goals

      <!--- What do you want to achieve? -->
      Sort documents by multiple field when using MongoDB collection find or aggregate.
      e.g. {$sort: {createdAt: -1, username: 1}}

      Expected Results

      <!--- What did you expect to happen? -->
      First sort by createdAt in descending order and then sort by username in ascending order.

      Actual Results

      <!--- What happened instead?
      e.g. the stack trace of a crash
      -->
      Since Dictionary in Swift or NSDictionary in Objective-C is unordered collection, the order of {$sort: {createdAt: -1, username: 1}} is uncertain, it could not make sure the consistency of sort order.

      For example, using FindOptions for finding document

      let options = FindOptions(limit: 20, projection: nil, sort: ["createdAt": -1, "username": 1])
      collection.find(filter: ["favorite": "MongoDB Realm"], options: options)
      

      This result will be not consistent, because the result could be sorted by createdAt than username or by username than createdAt.

      Steps for others to Reproduce

      <!--- What are steps OTHERS can follow to reproduce this issue? -->

      Code Sample

      <!---
      Provide a code sample or test case that highlights the issue.
      If relevant, include your model definitions.
      For larger code samples, links to external gists/repositories are preferred.
      Alternatively share confidentially via mail to help@realm.io.
      Full Xcode projects that we can compile ourselves are ideal!
      -->

      Version of Realm and Tooling

      <!---
      In the CONTRIBUTING guidelines, you will find a script,
      which will help determining some of these versions.
      -->
      Realm framework version: 10.7.2

      Xcode version: 12.4

      iOS/OSX version: 14.4

      Dependency manager + version: Swift Package Manager

            Assignee:
            diana.perez@mongodb.com Diana Maria Perez Afanador (Inactive)
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: