0

Context: I saved a list of objects in realm repository. These objects contain a property of standard type Date. I would like to get a list of those objects back from Realm based on distinct dates which means in other words : If two objects in realm have the same date, I only want to return the first object and not the second one. If 4 objects have the same date, I only want to return the first one and not the three others. So at the end, my final result from Realm will only contain a list of objects with distinct/unique dates. Basically I could simply use this line of code :

realm.objects(type.self).distinct(by: [key])

where :

  • type is the type of the object itself
  • key is a string representing the key path of desired date property.

My problem: I would like to compare dates based only on Day value : if two dates have the same day value they should be considered the same date, else they should be considered different dates. However, since Date is part of Foundation library I can't change the == method to specify my own comparison result.

I have tried to create a sort descriptor with my own comparator using this initialiser :

NSSortDescriptor(key: String , ascending: bool, comparator: Comparator)

where comparator could be a closure returning my own Comparison result. However I can't sort realm results with NSSortDescriptor directly, I need to convert it to SortDescriptor. SortDescriptor can be initialised with NSSortDescriptor like this:

let nsSortDescriptor = NSSortDescriptor(key: myKey, ascending: true, comparator: myComparator)
let sortDescriptor = SortDescriptor(keyPath: nsSortDescriptor.key , ascending: nsSortDescriptor.ascending)

As you can see, this initialiser does not provide a comparator input parameter which means I can't specify my own comparator inside SortDescriptor initialiser. How can I use my own comparator inside SortDescriptor ?

Red
  • 1
  • Welcome to SO. The question needs clarification and the code you've attempted, as we are not a code-writing service. First this is unclear *saved a list of objects in realm repository* - repository? Do you mean saved objects in Realm? Is this a local or sync'd Realm? Then, Realm objects have no guarantee of sort order unless you specify the sort with a query. So, *first* today may not be *first* tomorrow. Then *based only on Day value* - what do you consider "day value"? Is Sunday 0, Monday 1 etc? Or day of the year? Or something else? Can you store the "Day value" in a separate property? – Jay Jun 12 '23 at 20:29
  • The question needs a bit more clarity for us to understand the use case. Once we have that clarity, we may be able to assist. If you can address the comment by providing info as to what is meant by 'repository' when it comes to storing Realm data (as it's not stored in a repository), is this a local or sync'd realm and what is meant by 'first' in your context, that would be a help. Any minimal example code would also be helpful - take a look at [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Jay Jun 13 '23 at 19:42

0 Answers0