1 2 3 Previous Next 39 Replies Latest reply on Nov 10, 2008 5:30 PM by 807589

    Sorting a list of Objects after Objects integer.

    807589
      Hey all.

      I want to sort a list of objects(java.awt.Point). I want to sort after the Point.x. I've tried search forum, but no luck of finding my problem. I always error. Please see my code.{
      public class Main {
      
          public static void main(String[] args) {
              List<Point> pl = new ArrayList<Point>();
              pl.add(new Point(2,1));
              pl.add(new Point(1,1));
              pl.add(new Point(4,1));
              pl.add(new Point(3,1));
              
              for(int i = 0; i < pl.size(); i++){
                  System.out.print(pl.get(i).x);
              }
              
              Collections.sort(pl); //<---------------------THE PROBLEM IS HERE.
              
              for(int i = 0; i < pl.size(); i++){
                  System.out.print(pl.get(i).x);
              }
          }
      }
        • 1. Re: Sorting a list of Objects after Objects integer.
          JoachimSauer
          Collections.sort(List) can only sort collections that contain objects that implement Comparable.

          Point doesn't implement Comparable because Points don't have a natural order (which Point is bigger: (0,1) or (1,0)?)

          You need to define your own Comparator that defines how you'd like to compare your points and pass that to Collections.sort() (the version that takes a List and a Comparator, obviously).
          • 2. Re: Sorting a list of Objects after Objects integer.
            807589
            Can i use something else to sort the Points after the x positition?
            • 3. Re: Sorting a list of Objects after Objects integer.
              JoachimSauer
              HanogluX wrote:
              Can i use something else to sort the Points after the x positition?
              Yes, you can write a Comparator that does that.

              As I already said in my post above.
              • 4. Re: Sorting a list of Objects after Objects integer.
                JoachimSauer
                Question: Did you read my reply only until the first part that sounded complicated? Because I answered your exact question already and I wonder what I can do to make my answers more digestable in the future.
                • 5. Re: Sorting a list of Objects after Objects integer.
                  807589
                  The code i show you is just for test.

                  Actually i've my own object. In that object i got a int, i want to sort after that integer.
                  I never used a Comparator before, so you need to help me.
                  • 6. Re: Sorting a list of Objects after Objects integer.
                    807589
                    HanogluX wrote:
                    I never used a Comparator before, so you need to help me.
                    You can help yourself by studying the Java™ Tutorial on [Object Ordering|http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html]. It has detailed instruction on how to use a Comparator.

                    ~
                    • 7. Re: Sorting a list of Objects after Objects integer.
                      807589
                      Yeah, i didnt understand anything of it, actually :)
                      • 8. Re: Sorting a list of Objects after Objects integer.
                        807589
                        HanogluX wrote:
                        Yeah, i didnt understand anything of it, actually :)
                        Nothing at all? I find that hard to believe. Nevertheless, if you understand nothing of an introductory tutorial, how do you expect an explanation on a technical forum to be of assistance?

                        ~
                        • 9. Re: Sorting a list of Objects after Objects integer.
                          JoachimSauer
                          HanogluX wrote:
                          I never used a Comparator before,
                          fair enough.
                          so you need to help me.
                          I accept your premise but I disagree with your conclusion.

                          If someone tells you to use a Comparator and you don't know how to use it, then you first have to try to find out how to use it (Google, read JavaDoc, find tutorials) and then if you've got problems and/or questions you can come back.
                          • 10. Re: Sorting a list of Objects after Objects integer.
                            807589
                            Hi, you need to do this:

                            the method Collections.sort() needs a second argument... that defines how the points will be compare.

                            Example:
                            import java.awt.Point;
                            import java.util.Comparator;
                            
                            class PointSorter implements Comparator
                            {
                                public int compare(Object p_firstObject, Object p_secondObject)
                                {        
                                    Point firstPoint=(Point)p_firstObject;
                                    Point secondPoint=(Point)p_secondObject;
                                    
                                    int result=1;
                                    
                                    if (firstPoint.x<secondPoint.x)
                                        result=-1;
                                    else if (firstPoint.x==secondPoint.x)
                                        result=0;
                                    
                                    return result;
                                }
                            }
                            then... if you want to sort the points by x:
                            public static void main(String[] args) 
                            {
                                    List<Point> pl = new ArrayList<Point>();
                                    pl.add(new Point(2,1));
                                    pl.add(new Point(1,1));
                                    pl.add(new Point(4,1));
                                    pl.add(new Point(3,1));
                                    
                                    for(int i = 0; i < pl.size(); i++)
                                    {
                                        System.out.print(pl.get(i).x+" - ");
                                    }
                                    
                                    Collections.sort(pl, new PointSorter());
                                    
                                    for(int i = 0; i < pl.size(); i++)
                                    {
                                        System.out.print(pl.get(i).x+" - ");
                                    }
                            }
                            • 11. Re: Sorting a list of Objects after Objects integer.
                              807589
                              yawmark wrote:
                              Nevertheless, if you understand nothing of an introductory tutorial, how do you expect an explanation on a technical forum to be of assistance?
                              Ah, I forgot about the old "Wait Around For Someone To Write It For You"™ technique. Silly me.

                              ~
                              • 12. Re: Sorting a list of Objects after Objects integer.
                                807589
                                I hate to get helped on the internet. Someone will always blame you, because you dont have learned how to a use Comparator or something else.

                                And dont say i was waiting for the code. I just needed an example of it. So i can understand it.
                                • 13. Re: Sorting a list of Objects after Objects integer.
                                  807589
                                  Given that there are two ways that the points could be sorted (by X and by Y) I don't think that it's appropriate to implement the comparable interface here.

                                  It would probably be better to implement a static Main.sortByX() method.
                                  • 14. Re: Sorting a list of Objects after Objects integer.
                                    JoachimSauer
                                    HanogluX wrote:
                                    I hate to get helped on the internet. Someone will always blame you, because you dont have learned how to a Comparator or something else.
                                    Yes, it's really evil, isn't it? People expect you to do a google search and read at least one link in there before you request a complete re-write of tutorials that are already available elsewhere.

                                    Why don't people realize that your time is simply so much more valuable than ours that you can't be bothered to google.

                                    There are two kinds of questions around here(*):

                                    1.) General, unspecific questions that are not really suited for a forum, because they are much better answered by one of the many, many tutorials out there that can easily be found by google.
                                    2.) Specific questions where the poster makes clear that he tries to understand something and states clearly where exactly he has a problem and which information he doesn't understand.

                                    Guess which one is more fun to answer.

                                    If you want to be able to write a question of the second kind, then I suggest you learn [How To Ask Questions The Smart Way|http://www.catb.org/~esr/faqs/smart-questions.html].

                                    (*) There are more, of course, but I'll focus on these first.
                                    And dont say i was waiting for the code. I just needed an example of it. So i can understand it.
                                    There are tons of exampels out there. If you have specific question: fine, go ahead and ask it. If you're just using us as a Google-Replacement: Go Away!

                                    regards
                                    1 2 3 Previous Next