Insert sort for object

Source: Internet
Author: User

/* * Objectsort.java * Demonstrate sorting objects (use insertion sort) */public class Person {private String Firstnam    E    Private String LastName;        private int age;        Constructor public Person (string lastname, string firstname, int age) {this.lastname = LastName;        This.firstname = FirstName;    This.age = age;    }//Get LastName public String GetLast () {return LastName;        }//Display public void Displayperson () {System.out.print ("Lastname:" + Lastname);        System.out.print ("Firstname:" + Firstname);    System.out.print ("Age:" + age);    }}//end Class person//---------------------------public class Arrayinob {private person[] A;        private int nelems;        Constructor public arrayinob (int max) {a = new Person[max];    Nelems = 0; }//Put element to array public void Insert (string lastname, String firstname, Int. age) {A[nelems] = NE W person (LastName, FirsTname, age);    nelems++; }//Display public void display () {for (int j = 0; J < Nelems; J + +) {A[j].displayperson (            );        System.out.println ("");                }}//insert sort public void Insertsort () {int in, out;            for (out = 1, out < Nelems, out++) {Person temp = a[out];                        in = out;            int compare = A[in-1].getlast (). CompareTo (Temp.getlast ());                while (in > 0 && compare > 0) {//Shift item to right a[in] = a[in-1];                       Go left one position--in;        }//Insert marked item a[in] = temp; }}}//End Class arrayinob//---------------------------public class Objectsortapp {public static void main (STR        Ing[] args) {int maxSize = 100;        Arrayinob arr;                arr = new Arrayinob (maxSize); Put elements inTo array Arr.insert ("Evans", "Zatty", 24);        Arr.insert ("Bvans", "Aatty", 21);        Arr.insert ("Avans", "Yatty", 5);                Arr.insert ("Tvans", "Xatty", 99);        System.out.println ("Before sorting:");                Arr.display ();        System.out.println ("After sorting:");        Arr.insertsort ();    Arr.display (); }}

Insert sort of object

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.