Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort results is different between realm sort and Swift sort #8451

Closed
yzm347404 opened this issue Jan 5, 2024 · 2 comments
Closed

Sort results is different between realm sort and Swift sort #8451

yzm347404 opened this issue Jan 5, 2024 · 2 comments
Assignees
Labels
More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. O-Community Waiting-For-Reporter Waiting for more information from the reporter before we can proceed

Comments

@yzm347404
Copy link

i am using realmSwift, to cater the requirement, i will sort this result for display, when i sort it using sorted(by:) in realm and sorted() in swift, found that the results is different. The sort keyword is a property of the object type String.

func sortInventories(inventories: Results, by preference: SortingPreference) {
var sortDescriptors = RealmSwift.SortDescriptor
sortDescriptors.append(.init(keyPath: \Inventory.brandName))
var arr = Array(inventories).sorted { s, ss in
return s.brandName < ss.brandName
}
var xx = inventories.sorted(by: sortDescriptors)
}

image

@Jaycyn
Copy link

Jaycyn commented Jan 5, 2024

There are a number of reasons those sorts will be different and I don't believe this line of code is valid as-is

var sortDescriptors = RealmSwift.SortDescriptor

But... there's no need to leverage high level Swift functions - and doing that defeats the purpose of the memory friendly lazy-loading feature of Realm.

Why not just use a one liner?

let sortedInventory = realm.objects(Inventory.self).sorted(by: \.brandName)

@sync-by-unito sync-by-unito bot added More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Feb 5, 2024
Copy link

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
More-information-needed More information is needed to progress. The issue will close automatically in 2 weeks. O-Community Waiting-For-Reporter Waiting for more information from the reporter before we can proceed
Projects
None yet
Development

No branches or pull requests

3 participants