How to sort a list by property of objects in a collection?

So, I put up with the fact that I could not reliably use the list order, because sleep mode changes it, and everyone says that do not do this, so I added the field to my class as a position. I have:

@Entity
class Procedure {
...
int procedureId;
List<Task> tasks;
...
}

@Entity
class Task {
...
int taskId;
int position;
}

Now I do not know how to approach the interaction with the list. Should I just sort it by position, when I first get it from db and start working with it, and then I can leave all the code change code that I already wrote, and then just reset all positions to save, order the list so that I could have resorted when i get back?

SKIP TO THE ACTUAL QUESTION HERE:

This seems to be the best approach, but HOW do I sort the list by the property of the objects in the collection?

+3
2

, ,

java.util.Collections.sort(tasks, new Comparator<Task>() {
@Override
public int compare(Task t1, Task t2) {
    return t1.getPosition() - t2.getPosition();
});

Yuval = 8 -)

+5

, Hibernate, , , <list-index> , 6.2.3 Indexed Collections, , .

, Java . Hibernate, Hibernate .

+3

Source: https://habr.com/ru/post/1708974/


All Articles