Rewrite of the sort method comparator in the "Java" collections

Source: Internet
Author: User

A lot of people just use the sort method without the comparator comparator in the collections to do some simple sorting of the dynamic array ArrayList that stores the shaping integer, including me before, just before the "Java" The collections class--java in Java, the data structure in the upgraded version (click Open link), describes the simple use of the sort method in collections.

Igz

In the face of Eclipse's instructions, I don't know how to rewrite this sort method, and if I have a dynamic array of stored student classes based on the student's number, the names are sorted according to the order.

As the following definition:

Class Student {public int s_no;public String s_name;public int s_class;}
After initializing the dynamic array ArrayList that holds the student:

arraylist<student> Studentarr = new arraylist<student> (); Student S1 = new Student () S1.s_no = 3;s1.s_name = "a"; S1.s_class = 102;studentarr.add (S1); Student s2 = new Student () S2.s_no = 2;s2.s_name = "B"; s2.s_class = 101;studentarr.add (s2); Student s3 = new Student () S3.s_no = 1;s3.s_name = "C"; s3.s_class = 103;studentarr.add (S3);
Make the following effects:


In fact, it is very simple to rewrite the comparator interface in sort. The sort method is difficult because one of the parameters is the interface, which has never been touched before, but this thing is and "JavaScript" using the sort () function and file fragment implementation of the table of the front-end sort, compatible with the IE6 of the original ecology (Click to open link) JavaScript's soft method, like the definition of collation, set a good return value, this can not understand things, immediately become a sort of sharp weapon!

A program that is clear:

Import java.util.*;//The following is the student class student definition, a bit like the C language of the structural body Ah! ^_^ class Student {public int s_no;public String s_name;public int s_class;} public class Comparetest {public static void main (string[] args) {//The initialization of a dynamic array that holds student classes arraylist<student> Studentarr = n EW arraylist<student> (); Student S1 = new Student () S1.s_no = 3;s1.s_name = "a"; S1.s_class = 102;studentarr.add (S1); Student s2 = new Student () S2.s_no = 2;s2.s_name = "B"; s2.s_class = 101;studentarr.add (s2); Student s3 = new Student () S3.s_no = 1;s3.s_name = "C"; s3.s_class = 103;studentarr.add (S3);// After initialization, print the following dynamic array System.out.println ("before Ordering:"); for (int i = 0; i < studentarr.size (); i++) {System.out.println ("I am" + stud Entarr.get (i). S_class + "class" + Studentarr.get (i). S_name + "study number is" + studentarr.get (i). S_No);} Overrides for the comparator interface//This interface is an abstract function, the given parameters and the return value are dead. Collections.sort (Studentarr, New comparator<object> () {public int compare (object O1, Object O2) {//You first set what you want to compare// Specifically, the argument is cast to the object you want to compare, here is the two student class//Here the S1,S2 with the above s1,s2 a little relationship is not, just abstractThe relationship between the former and the latter Student S1 = (Student) O1; Student s2 = (Student) o2;//If the former number is greater than the latter's number, is the former greater than the latter, the return 1 system will recognize that the former is greater than the latter if (S1.s_no > S2.s_no) {return 1;} Less than the same if (S1.s_no < s2.s_no) {return-1;} If return 0 considers the former equal to the latter return 0;}}); /compare and then output the result after the System.out.println ("Sort by study number:"); for (int i = 0; i < studentarr.size (); i++) {System.out.println ("I Am" + Studentarr.get (i). S_class + "class" + Studentarr.get (i). S_name + "study number is" + studentarr.get (i). S_No);} The following is a class-ordered process Collections.sort (Studentarr, New comparator<object> () {public int compare (object O1, Object O2) { Student S1 = (Student) O1; Student s2 = (Student) o2;if (S1.s_class > S2.s_class) {return 1;} if (S1.s_class < s2.s_class) {return-1;} return 0;}}); System.out.println ("Sorted by class:"); for (int i = 0; i < studentarr.size (); i++) {System.out.println ("I Am" + studentarr.get (i) . S_class + "class" + Studentarr.get (i). S_name + "study number is" + studentarr.get (i). s_no);}}

(1) At the outset, the dynamic array of students is in a disorderly order. Don't ask me why the initialization is not first ordered, if you read from the file or database content, you simply have no way to control their order, you must go through the Sort method, set the rules themselves, the data in the dynamic array to sort

(2) If the rule is not set, that is, sort does not rewrite the comparator comparator interface, the system will not be intelligent to recognize, "I want to follow each student's school number to sort." The system will only feel that this object is placed in the memory location XXX, the object is placed in the memory location yyy, and then based on the memory of our human simply meaningless location before and after the order.

Rewrite of the sort method comparator in the "Java" collections

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.