Sort by a few lines of code

xiaoxiao2021-03-06  117

Blundblade [original]

Abstract: I often see some people still use bubbling (most common!) Or exchange to sort, in fact, using a few lines of code, can be sorted quickly, whether it is simple type or class, array or Java aggregation COLLECTION.

Simple type sort

Simple types are not external to BYTE, CHAR, SHORT, INT, Long, Float, Double and other data types, these types cannot be placed in aggregates, and only arrays can only be used. The Java.util.Arays method provides the Sort method for these types (actually there are many other useful methods), below is sorted for a simple int array:

Int [] Arr = {2, 3, 1, 10, 7, 4};

System.out.print ("Before Sort:");

For (int i = 0; i

System.out.print (Arr [i] "");

SYSTEM.OUT.PRINTLN ();

Arrays.sort (Arr);

System.out.print ("After Sort:");

For (int i = 0; i

System.out.print (Arr [i] "");

SYSTEM.OUT.PRINTLN ();

Output results:

BEFORE SORT: 2 3 1 10 7 4

After Sort: 1 2 3 4 7 10

We see the resulting result is to be arranged in ascending order, the following sort is the case.

Object sort

The object can be placed in an array, and arrays.sort (Object [] arr) is also called; can also be placed in the gathering, using java.util.collections sort (List List). Note that not List must implement a list interface, not just a Collection interface.

But this class must implement a java.lang.comParable interface. This interface has only one way: int CompartTo (Object O), returns a positive integer when this object is larger than the incoming object. Take the class programer as an example:

Class Programmer IMPLEments Comparable {

PRIVATE STRING NAME;

PRIVATE STRING LANGUAGE;

PRIVATE DOUBLE PAY;

Public Programmer (String Name, String Language, Double Pay) {

THIS.NAME = Name;

THIS.LANGUAGE = Language;

THIS.PAY = PAY;

}

Public int compareto (Object O) {

Programmer other = (programmer) O;

Return (int) pay - (int) other.pay;

}

Public string toString () {

Return "{name:" Name ", Language:" Language ", Money:" Pay "}";

}

}

Sort it:

ArrayList List = New ArrayList ();

List.Add (New Programmer, "C", 12000); List.Add (New Programmer ("Li Si", "Java", 200);

List.add (New Programmer ("Wang 5", "C ", 5000);

List.add (New Programmer ("Money Six", "VB", 3000);

System.out.println ("Before Sort:" LIST);

Collections.sort (List);

System.out.println ("After Sort:" LIST);

Output:

Before Sort: [{Name: Zhang San, Language: C, Money: 12000.0}, {Name: Java, Money: 200.0}: C , Money: 5000.0}, { Name: Qian Liu, Language: VB, Money: 3000.0}]

After Sort: [{Name] Language: Java, Money: 200.0}, {Name: Money Six, Language: VB, Money: 3000.0}, {Name: Wang 5000.0}, Money: 5000.0}, { Name: Zhang 3, Language: C, Money: 12000.0}]

Simple enough! The JavaDoc, and the case, many classes have implemented the interface, so the sorting of these classes can be determined.

Recently, I recently sorted in the array with System.Array.Sort, which is suitable for all objects that implement the IComparable interface. It seems that Microsoft's learning ability is really strong!

Sort the existing class

The above method has a problem, that is, a class already exists, and does not implement the Compailable interface, use a subclass to encapsulate? Capable (you can try the example below). There is also a situation that a variety of sorting is impossible for a class. Taking the File class as an example, it implements the Comparable interface, but is sorted by name. If you want to sort in size, or sort it according to the modification time? For these two situations, use the COMPARATOR interface of the java.util package:

Arrays.Sort (Object [] Arr, Comparator COM)

Collectes.Sort (Object [] Arr, Comparator COM)

Comparator interface method:

Public int COMPARE (Object O1, Object O2) When the O1 is larger than o2 larger time to return a positive integer

Public Boolean Equals (Object Obj) Judging whether the OBJ is the same object of this Comparator

Below with the Comparator to sort file size or modification time:

Class fileUtils {

Static class compratorbylastmodified imports comparator {

Public int Compa (Object O1, Object O2) {

FILE FILE1 = (file) O1;

File file2 = (file) O2;

Long Diff = file1.lastmodified () - file2.lastmodified (); if (diff> 0)

Return 1;

Else IF (DIFF == 0)

Return 0;

Else

Return -1;

}

Public Boolean Equals (Object Obj) {

Return true; // Simple practice

}

}

Static Class CompratorBysize Implements Comparator {

Public int Compa (Object O1, Object O2) {

FILE FILE1 = (file) O1;

File file2 = (file) O2;

Long Diff = file1.Length () - file2.length ();

IF (DIFF> 0)

Return 1;

Else IF (DIFF == 0)

Return 0;

Else

Return -1;

}

Public Boolean Equals (Object Obj) {

Return true; // Simple practice

}

}

}

Calling example:

FILE DIR = New File ("C: // Temp");

File [] files = dir.listfiles ();

System.out.print ("Before Sort:");

For (int i = 0; i

System.out.print (files [i] ");

SYSTEM.OUT.PRINTLN ();

Arrays.sort (files);

System.out.print ("Sort by Name:");

For (int i = 0; i

System.out.print (files [i] ");

SYSTEM.OUT.PRINTLN ();

Arrays.Sort (files, new fileutils.compratorbysize ());

System.out.print ("Sort by size:");

For (int i = 0; i

System.out.print (files [i] ");

SYSTEM.OUT.PRINTLN ();

Arrays.Sort (Files, New FileUtils.compratorbylastModified ());

System.out.print ("Sort by Last Modified:");

For (int i = 0; i

System.out.print (files [i] ");

SYSTEM.OUT.PRINTLN ();

Let's find a directory. Use the methods in these Java class libraries, should you use your own sorting algorithm?

Finally, the full code is attached to the top:

Testsort.java

Import java.io. *;

Import java.util. *;

Public class tests {

Public static void main (String [] args) {

SortsImpleType ();

sortcomparable ();

sortcomparator ();

Public static void sortsimpletype () {

Int [] Arr = {2, 3, 1, 10, 7, 4};

System.out.print ("Before Sort:");

For (int i = 0; i

System.out.print (Arr [i] "");

SYSTEM.OUT.PRINTLN ();

Arrays.sort (Arr);

System.out.print ("After Sort:");

For (int i = 0; i

System.out.print (Arr [i] "");

SYSTEM.OUT.PRINTLN ();

}

Public static void sortcomparable () {

ArrayList List = New ArrayList ();

List.add (New Programmer ("Zhang San", "C", 12000);

List.Add (New Programmer ("Li Si", "Java", 200);

List.add (New Programmer ("Wang 5", "C ", 5000);

List.add (New Programmer ("Money Six", "VB", 3000);

System.out.println ("Before Sort:" LIST);

Collections.sort (List);

System.out.println ("After Sort:" LIST);

}

Public static void sortcomparator () {

FILE DIR = New File ("C: // Temp");

File [] files = dir.listfiles ();

System.out.print ("Before Sort:");

For (int i = 0; i

System.out.print (files [i] ");

SYSTEM.OUT.PRINTLN ();

Arrays.sort (files);

System.out.print ("Sort by Name:");

For (int i = 0; i

System.out.print (files [i] ");

SYSTEM.OUT.PRINTLN ();

Arrays.Sort (files, new fileutils.compratorbysize ());

System.out.print ("Sort by size:");

For (int i = 0; i

System.out.print (files [i] ");

SYSTEM.OUT.PRINTLN ();

Arrays.Sort (Files, New FileUtils.compratorbylastModified ());

System.out.print ("Sort by Last Modified:");

For (int i = 0; i

SYSTEM.OUT.PRINTLN ();

}

}

Class Programmer IMPLEments Comparable {

PRIVATE STRING NAME;

PRIVATE STRING LANGUAGE;

PRIVATE DOUBLE PAY;

Public Programmer (String Name, String Language, Double Pay) {

THIS.NAME = Name;

THIS.LANGUAGE = Language;

THIS.PAY = PAY;

}

Public int compareto (Object O) {

Programmer other = (programmer) O;

Return (int) pay - (int) other.pay;

}

Public string toString () {

Return "{name:" Name ", Language:" Language ", Money:" Pay "}";

}

}

Class fileUtils {

Static class compratorbylastmodified imports comparator {

Public int Compa (Object O1, Object O2) {

FILE FILE1 = (file) O1;

File file2 = (file) O2;

Long Diff = file1.lastmodified () - file2.lastmodified ();

IF (DIFF> 0)

Return 1;

Else IF (DIFF == 0)

Return 0;

Else

Return -1;

}

Public Boolean Equals (Object Obj) {

Return true; // Simple practice

}

}

Static Class CompratorBysize Implements Comparator {

Public int Compa (Object O1, Object O2) {

FILE FILE1 = (file) O1;

File file2 = (file) O2;

Long Diff = file1.Length () - file2.length ();

IF (DIFF> 0)

Return 1;

Else IF (DIFF == 0)

Return 0;

Else

Return -1;

}

Public Boolean Equals (Object Obj) {

Return true; // Simple practice

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-125709.html

New Post(0)