10 Replies Latest reply: Mar 20, 2007 6:43 AM by 800282 RSS

    Ordering a list

    807606
      Hello i would like to order a list of object by a number set by a property of the object contain in the list. For exemple i have a list containing some person object, each person has a property numOrder, so that if numOrder equal 1 the person will be place first on list.
      Thanks for helping.
        • 1. Re: Ordering a list
          807606
          and what have you done to do this?
          • 2. Re: Ordering a list
            807606
            I have a list of person and i would like to create another list containing those person but ordered by their property numOrder.
            If someone can help.

            Message was edited by:
            JusteUneQuestion
            • 3. Re: Ordering a list
              800282
              Let that Person class implement the Comparable interface, write the compareTo(): int method, and call java.util.Collections.sort(List<Person> yourList);.
              Done.
              • 4. Re: Ordering a list
                807606
                http://java.sun.com/javase/6/docs/api/java/util/Comparator.html
                http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)
                • 5. Re: Ordering a list
                  807606
                  Can't you sort the list when you create it ? Did you have a look to the SortedMap interface ?
                  • 6. Re: Ordering a list
                    800282
                    Can't you sort the list when you create it ? Did you
                    have a look to the SortedMap interface ?
                    You can't just stuff them into it: that Person class will still needs to implement the Comparable interface in that case, of course (perhpas you know this, then it's just a note to the OP).
                    • 7. Re: Ordering a list
                      807606
                      Yes i understand my reply was not clear enought. As the API says (if i remember good), a SortedMap needs an implemented comparator when the map is created.
                      But if the numOrd is a string then I think that you can use it as a key because String implements Comparator.

                      Well i hope i am not out of subject ;)
                      • 8. Re: Ordering a list
                        800282
                        Yes i understand my reply was not clear enought. As
                        the API says (if i remember good), a SortedMap needs
                        an implemented comparator when the map is created.
                        Not quite.
                        Either:
                        - you need to provide a Comparator as a parameter to the constructor of the implementation of a SortedMap (a TreeMap for example);
                        - or have your Person class implement implement the Comparable interface.

                        But the OP is talking about a List, and s/he might not want a map, which has a key-value pair.

                        But if the numOrd is a string then I think that you
                        can use it as a key because String implements
                        Comparator.
                        No, it implements Comparable, not Comparator.

                        Well i hope i am not out of subject ;)
                        Just a bit. Don't be afraid to look things up in the API if you're not sure.
                        ; )
                        • 9. Re: Ordering a list
                          807606
                          Not quite.
                          Either:
                          - you need to provide a Comparator as a parameter to
                          the constructor of the implementation of a SortedMap
                          (a TreeMap for example);
                          - or have your Person class implement implement the
                          Comparable interface.
                          Correction: the Comparator is optional, and as the sort number value probably implements Comparable (cause it's a String or Number), it should indeed work by putting each Person object as value and its sort value as key into the (Tree)Map. However, I also consider implementing Comparable in Person as the better solution.
                          • 10. Re: Ordering a list
                            800282
                            ...
                            Correction: the Comparator is optional, and as the
                            sort number value probably implements Comparable
                            (cause it's a String or Number), it should indeed
                            work by putting each Person object as value and its
                            sort value as key into the (Tree)Map. However, I also
                            consider implementing Comparable in Person as the
                            better solution.
                            You're right.
                            I should've stated that the key's are being used to sort the map. And it's probably a bad idea to use a Person as a key.