objectbox-java
objectbox-java copied to clipboard
Order ToMany objects
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;
}
hi
how can i sort relations ? im using kotlin in android
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
Thanks
But when should i set it ? Before accessing the data? Before storing? I need to set it up every time ?
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 i found OrderBy annotation in source code, sorting is implement or not yet ?
Related issue: #243.