0

With a series of object tasks performed asynchronously but needed to be displayed in precise order how can they be sorted and displayed with Thymeleaf. There will be HashSet<StoreTask> GlobalTasksDTO but need to be displayed within each store listing as Opening:Security:Lighting:Departments per store. Any insight or directions.

public class StoreTask{
    Store store;
    Lighting lighting;
    Security security;
    Departments departments;
    Opening opening;
}
cp.
  • 1,241
  • 5
  • 15
  • 26
  • 1
    Thymeleaf will iterate in the order supported by the specific Java collection you are using. For a `HashSet`, that order is undefined. In your case, consider using a `TreeSet` instead - or, maybe simpler, an `ArrayList`, for the Thymeleaf template's model. See [How to sort a HashSet?](https://stackoverflow.com/q/22391350/12567365) Then implement `Comparator` or `Comparable` to define your sort order. [How to compare objects by multiple fields](https://stackoverflow.com/q/369512/12567365) – andrewJames Jun 03 '22 at 14:32
  • Thymeleaf does have a couple of [list utility methods](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#lists) for sorting, but in your case, you may as well do the sort explicitly in your Java code up-front (in my opinion). – andrewJames Jun 03 '22 at 14:39
  • The ArrayList will set the order but how to I store the heterogeneous objects? Further what is the Thymeleaf syntax to loop and to print the different members of the different objects? Is each object displayed in a form? Is Thymeleaf syntax generic enough to call the same getters across different object types? – cp. Jun 03 '22 at 14:58
  • The array list will be an list of store tasks `List`. Each field in `StoreTask` which is a custom object (`Store`, `Lighting`, etc.) will need its own comparable to support overall sorting of `StoreTask` objects. The Thymeleaf syntax for looping can be found in the official [documentation for iteration](https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#iteration). – andrewJames Jun 03 '22 at 15:20
  • I am not sure what you mean by "generic enough" regarding "the same getters and setters". Why would you not know the type of object you are handling? If you want to display some data for a `Lighting` object (for example), you have to pick the relevant getter(s) for the relevant field(s). Thymeleaf is only as capable as Java in this regard - since Thymeleaf uses Java for all of this processing. Perhaps you want to do something which I have missed, or which is not clear from the wording in the question. If so, you can clarify the question - maybe with more details and an example. – andrewJames Jun 03 '22 at 15:20

0 Answers0