java - Collections.sort isn't sorting -
i building web application using java ee (although problem more java based)
in servlet, getting list of orders ejb. in list of orders, there list of states order (sent, on dock, non received ...)
i want sort list of states date of state. use collections.sort this:
(command c : commands) { c.getstatelist().sort(new comparator<state>() { @override public int compare(state o1, state o2) { return o1.getstatedate().compareto(o2.getstatedate()); } }); c.getstatelist().sort(collections.reverseorder()); } request.setattribute("commands", commands);
but when display results, states not sorted.
i tried reverse order can see, isn't working either.
as can see, replaced collections.sort listiwanttosort.sort. still not working.
any ideas on why not work or how repair it?
edit : here getter list , instanciation :
@onetomany(cascade = cascadetype.all, mappedby = "ciicommande") private list<etat> etatlist; @xmltransient public list<etat> getetatlist() { return etatlist; } list<commande> commandes = new arraylist<commande>();
and commands findall method.
to display them, use :
<c:foreach items="${commandes}" var="cmd"> <td>${cmd.etatlist[0].codestatut.libellesituation}</td> </c:foreach>
you first sorting list using custom comparator. re-sorting according reversed natural ordering of elements - not custom ordering applied. first sort not taking effect list re-ordered second sort. note collections.reverseorder()
not reverse list - reverse of natural ordering (so elements in getetatlist()
must comparable
).
try losing second sort , doing:
c.getetatlist().sort(new comparator<etat>() { @override public int compare(etat o1, etat o2) { // note o2/o1 reversed. return o2.getdateetat().compareto(o1.getdateetat()); } });
Comments
Post a Comment