java - android how to collections.sort the object? -


this question has answer here:

now have 3object

user[0] = new user("a", "a"); user[1] = new user("b", "b"); user[2] = new user("c", "c"); 

there have many variable in object such as: age, name, email

then program running.....

user[0].setage(6); user[1].setage(5); user[2].setage(7); 

how can young old user name?

i want return: b,a,c

here class

public class user{     int age;     string name;     string email;      public user(string name, string email){         this.name = name;         this.email = email;      }      public int getage(){         return x;     }      public void setage(int age){         this.age = age;     }  } 

you can either make user comparable , have it's natural ordering age:

public class user implements comparable<user> {     // snipped...      @override     public int compareto (user other) {         return integer.compare(age, other.age);     } } 

or explicitly specify comparator when you're sorting:

collections.sort(users, new comparator<user>() {     @override     public int compare(user a, user b) {         return integer.compare(a.getage(), b.getage());     } }); 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -