Java sorting of Array objects

Source: Internet
Author: User

The following is a set of code to sort the array objects:

 PackageCom.sun;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.Collections;ImportJava.util.Comparator;Importjava.util.List; Public classTest09 {/**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubDog dog[]=NewDog[3]; dog[0]=NewDog ("Wangchai", 10); dog[1]=NewDog ("Laifu", 9); dog[2]=NewDog ("ww", 20);        Arrays.sort (dog);  for(inti=0;i<dog.length;i++) {System.out.println (dog[i].name); }    }}classDogImplementscomparable{ PublicInteger Tizhong = 0;  PublicString name = "";  PublicDog (String name,intTizhong) {         This. Name =name;  This. Tizhong =Tizhong; }    //Sort by age@Override/*public int CompareTo (Object o) {//TODO auto-generated method Stub Dog s= (dog) o;    Return tizhong>s.tizhong?1: (tizhong==s.tizhong?0:-1); }*/    //Sort by name/*public int CompareTo (Object o) {dog s= (dog) o;             int Result=name.compareto (s.name);       return result; }*/    //If you first sort by number and then by character, write this:      Public intcompareTo (Object o) {Dog s=(Dog) o; intresult= tizhong>s.tizhong?1: (tizhong==s.tizhong?0:-1); if(0==result) {Result=Name.compareto (s.name); }             returnresult; }}

Java sorting of Array objects

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.