0

I have an ArrayList that is a list of a class I made called "Profile". This profile contains an integer called "tokens". How can I generate an instance of this arraylist but with it organizing them by which profile has the most Tokens?

Here is my Profile class

public class Profile 
{
    private String name;
    private int tokens;

    public String getName() { return this.name; }
    public int getTokens() { return this.tokens; }
}

And I have all the profiles stored in an ArrayList such as this...

public static List<Profile> PROFILES = new ArrayList<Profile>();

Any help would be most appreciated.

John Brooks
  • 69
  • 1
  • 8
  • See this: http://stackoverflow.com/questions/18441846/how-sort-a-arraylist-in-java – Tamas G. May 10 '15 at 01:00
  • The convention for using capital letters for the name of an instance field extends to field values that are expected to be constant. The requirements for this are that the field be 3 things: `static`, `final`, and immutable. Your List field is both mutable and nonfinal, so you should not name it with capitals. – scottb May 10 '15 at 07:29

2 Answers2

0

Your class Profile has to implement the Comparable interface and override its compareTo method, in which you can compare the tokens' value, like this:

public class Profile implements Comparable<Profile>
{
    @Override
    public int compareTo(Profile p) {
        return p.tokens < this.tokens;
    }
}

Edit:

    public class Profile implements Comparable<Profile>
    {
        @Override
        public int compareTo(Profile p) {
             if (p.tokens > this.tokens) { 
                 return -1; 
             } else if (p.tokens < this.tokens) { 
                 return 1; 
             } else 
                 return 0;
        }
    }

After that you are able to call Collections.sort(PROFILES);

Note that it is not recommended to write your variables with full capital letters. It would be better just simply Profiles

Tamas G.
  • 677
  • 3
  • 21
  • 38
-1
class Dog implements Comparator<Dog>, Comparable<Dog>{
   private String name;
   private int age;
   Dog(){
   }
   Dog(String n, int a){
      name = n;
      age = a;
   }
   public String getDogName(){
      return name;
   }
   public int getDogAge(){
      return age;
   }
   // Overriding the compareTo method
   public int compareTo(Dog d){
      return (this.name).compareTo(d.name);
   }
   // Overriding the compare method to sort the age 
   public int compare(Dog d, Dog d1){
      return d.age - d1.age;
   }
}

public class Example{

   public static void main(String args[]){
      // Takes a list o Dog objects
      List<Dog> list = new ArrayList<Dog>();

      list.add(new Dog("Shaggy",3));
      list.add(new Dog("Lacy",2));
      list.add(new Dog("Roger",10));
      list.add(new Dog("Tommy",4));
      list.add(new Dog("Tammy",1));
      Collections.sort(list);// Sorts the array list

      for(Dog a: list)//printing the sorted list of names
         System.out.print(a.getDogName() + ", ");

      // Sorts the array list using comparator
      Collections.sort(list, new Dog());
      System.out.println(" ");
      for(Dog a: list)//printing the sorted list of ages
         System.out.print(a.getDogName() +"  : "+
         a.getDogAge() + ", ");
   }
}

This would produce the following result:
Lacy, Roger, Shaggy, Tammy, Tommy,
Tammy  : 1, Lacy  : 2, Shaggy  : 3, Tommy  : 4, Roger  : 10,