Results 1 to 13 of 13
  1. #1
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Question Collections.sort

    Hey I have to sort out my ArrayList but cannot make it work. Here is
    Java Code:
        	Collections.sort(this.users);
        	 
        	User poor = this.users.get(0);
        	User ritch = this.users.get((this.users.size()-1));
        	  
            System.out.println("Poorest user has" + poor.getUsername()  + poor.getBalance() );
        	System.out.println("Ritches user has " + ritch.getUsername()  + poor.getBalance() + "\n");
    The Collections.sort(this.users); does not work. Any ideas why?

    Thanks!

  2. #2
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Default Re: Collections.sort

    And this is my ArrayList : ArrayList<User> users = new ArrayList<User>();

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Collections.sort

    Does you User implement the Comparable<User> interface? (otherwise Users can't be sorted)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Default Re: Collections.sort

    Thanks for your reply Jos!

    To be honest I am very new to programming so I have no idea what you are talking about :)

    Is what you are talking about something that should be in the ArrayList or in the sorting methode or in the code where I add information to my ArrayList?
    Thanks!

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Collections.sort

    Have you learned anything about interfaces and class inheritance so far? If not, you do not know enough about the base language features to dive into such topics as sorting.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Default Re: Collections.sort

    I know about class inheritance, but not about interfaces...

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Collections.sort

    Then this would be a good time to go study that.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Collections.sort

    Quote Originally Posted by qwe View Post
    Thanks for your reply Jos!

    To be honest I am very new to programming so I have no idea what you are talking about :)

    Is what you are talking about something that should be in the ArrayList or in the sorting methode or in the code where I add information to my ArrayList?
    Thanks!
    Suppose I show you a herring, an aardvark and a screwdriver and ask you which one is the 'largest' in a mathematical sense; unless you can compare those things, you can't answer this question. The same goes for your Users; i.e. they need to be Comparable in order to be sorted. A User needs to implement the Comparable<User> interface. There is an alternative but I don't want to confuse you even more ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Default Re: Collections.sort

    Hi again.
    I had this code before where it did print out the right numbers, the issue was that the name could not be printet as well. Thats why I changed the code. But this is the one from before.

    Java Code:
     
    	    	  ArrayList<User> users = server.getDatabase();
    	            	double max = 0.0;
    	            	double min = 100000;
    	            	 
    	            	 for(User acc: users){
    	            		 if(acc instanceof User){
    	            		 min = Math.min(min, acc.getBalance());
    	            		 max = Math.max(max, acc.getBalance());
    	            		 
    	            	System.out.println(acc.getUsername()+ "\t"+min);
    	            	System.out.println(acc.getUsername()+"\t"+max);
    	            		 
    	            		 
    	            		 }
    Now I wrote this in my class User.

    public class User implements Comparable {
    and my
    Java Code:
    User poor = this.users.get(0);
    works. But it does not compare......

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Collections.sort

    The Comparable<T> interface defines a method; did you implement it in your User class?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Collections.sort

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    qwe
    qwe is offline Member
    Join Date
    Nov 2014
    Posts
    13
    Rep Power
    0

    Default Re: Collections.sort

    The only thing I did in my class User was in the import part in the top where I wrote: public class User implements Comparable {

    From there on, im not really sure what to do.

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Collections.sort

    Quote Originally Posted by qwe View Post
    The only thing I did in my class User was in the import part in the top where I wrote: public class User implements Comparable {
    From there on, im not really sure what to do.
    Then you didn't check out the links I provided.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Selection sort to quick sort conversion without Nodes?
    By VettesRus in forum New To Java
    Replies: 0
    Last Post: 11-03-2013, 03:54 AM
  2. Replies: 9
    Last Post: 09-14-2012, 12:00 PM
  3. Problem arraylist result when use Collections.sort
    By i4ba1 in forum Advanced Java
    Replies: 4
    Last Post: 02-09-2011, 09:00 AM
  4. Collections.sort()
    By collin389 in forum Advanced Java
    Replies: 11
    Last Post: 12-31-2009, 12:03 PM
  5. Collections Sort
    By senthil_jr in forum Advanced Java
    Replies: 2
    Last Post: 06-04-2008, 09:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •