objectbox-java icon indicating copy to clipboard operation
objectbox-java copied to clipboard

Order ToMany objects

Open DJafari opened this issue 8 years ago • 6 comments

i think just like greendao for toMany relation need @OrderBy annotation example :

@Entity
public class Customer {
    @Id private Long id;

    @Backlink(to = "customerId")
    @OrderBy("date ASC")
    private ToMany<Order> orders;
}

DJafari avatar Aug 22 '17 22:08 DJafari

hi

how can i sort relations ? im using kotlin in android

Code-PLeX avatar Sep 24 '17 17:09 Code-PLeX

Since 1.0.1 there is an experimental feature to set a comparator on the ToMany that orders its items:

orders.setComparator(new Comparator<Order>() {
    @Override
    public int compare(Order left, Order right) {
        // TODO
    }
});

Let us know what you think.

edit: Note that support for SQL like order statements (like date ASC) is unlikely, as ObjectBox is not a SQL-based database. It is based on objects. -ut

greenrobot-team avatar Sep 26 '17 05:09 greenrobot-team

Thanks

But when should i set it ? Before accessing the data? Before storing? I need to set it up every time ?

Code-PLeX avatar Sep 26 '17 05:09 Code-PLeX

If you look at the code for ToMany the comparator is used when entities are first loaded into the ToMany. So just make sure to set the comparator before accessing or modifying the ToMany list.

edit: for example the constructor of your entity is a good place. -ut

greenrobot-team avatar Sep 26 '17 06:09 greenrobot-team

@greenrobot i found OrderBy annotation in source code, sorting is implement or not yet ?

DJafari avatar Oct 07 '17 12:10 DJafari

Related issue: #243.

greenrobot-team avatar Apr 17 '18 06:04 greenrobot-team