Comparable and comparator implementation object comparisons in Java

Source: Internet
Author: User

1. Sorting by comparable

Package Comparable;import Java.util.arrays;public class Comparableuser implements comparable<comparableuser> {p    Rivate String ID;    private int age;        Public Comparableuser (String ID, int age) {this.id = ID;    This.age = age;    } public int Getage () {return age;    public void Setage (int.) {this.age = age;    } public String GetId () {return id;    } public void SetId (String id) {this.id = ID;         } public int compareTo (Comparableuser o) {return this.age-o.getage ();    Return ((Comparableuser) O). Getage ()-this.age; }/** * Test method * * public static void Main (string[] args) {comparableuser[] users = new comparableuser[ ] {new Comparableuser ("u1001"), New Comparableuser ("u1002"), New Com        Parableuser ("u1003", 21)}; Arrays.sort (users);//Sort objects for (int i = 0; i < users.length; i++) {ComparableUser user = Users[i];        System.out.println (User.getid () + "" + user.getage ()); }    }}

2. Sorting by implementing comparator

Package Comparable;import Java.util.arrays;import java.util.comparator;class User {private String ID;        private int age;          Public User (String ID, int age) {this.id = ID;      This.age = age;      } public int Getage () {return age;      public void Setage (int.) {this.age = age;      } public String GetId () {return id;      } public void SetId (String id) {this.id = ID;          }}public class Usercomparator implements Comparator {public int compare (object arg0, object arg1) {      return (user) arg0. Getage ()-((user) arg1). Getage (); }/** * Test method * * public static void Main (string[] args) {user[] users = new user[] {new Us          ER ("u1001"), New User ("u1002"), New User ("u1003", 21)};          Arrays.sort (Users, New Usercomparator ()); for (int i = 0; i < users.length; i++) {User user = UseRs[i];          System.out.println (User.getid () + "" + user.getage ());   }      }}

Recommended Use cases:

Package Comparable;import Java.util.arrays;import java.util.comparator;class User {private String ID;    private int age;    private int score;        Public User (String ID, int age, int score) {this.id = ID;        This.age = age;    This.setscore (score);    } public int Getage () {return age;    public void Setage (int.) {this.age = age;    } public String GetId () {return id;    } public void SetId (String id) {this.id = ID;    } public int Getscore () {return score;    } public void SetScore (int score) {This.score = score; }}public class Usercomparator implements comparator<user> {public int compare (user arg0, user arg1) {if (        Arg0.getage () <arg1.getage ()) return 1;        else if (Arg0.getage () >arg1.getage ()) return-1;        else if (Arg0.getscore () <arg1.getscore ()) return-1; else if (Arg0.getscore () >arg1.getscore ()) return1;    return 0;                 }/** * Test method * * public static void Main (string[] args) {user[] users = new user[] {        New User ("u1001", "Max"), New User ("u1002", "25"), New User ("U1003", 91)};        Arrays.sort (Users, New Usercomparator ());            for (int i = 0; i < users.length; i++) {User user = Users[i];        System.out.println (User.getid () + "" + user.getage () + "" + User.getscore ()); }    }}

Comparable and comparator implementation object comparisons in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.