objectbox / objectbox-java

Android Database - first and fast, lightweight on-device vector database
https://objectbox.io
Apache License 2.0
4.41k stars 302 forks source link

Order ToMany objects #154

Open DJafari opened 7 years ago

DJafari commented 7 years ago

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;
}
Code-PLeX commented 7 years ago

hi

how can i sort relations ? im using kotlin in android

greenrobot-team commented 7 years ago

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

Code-PLeX commented 7 years ago

Thanks

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

greenrobot-team commented 7 years ago

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

DJafari commented 7 years ago

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

greenrobot-team commented 6 years ago

Related issue: #243.