Java collection Sorting method use of sort

Source: Internet
Author: User
Tags comparable

Transfer from http://blog.csdn.net/a1165117473/article/details/6965652

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

Package com.city.test;

Import Java.util.Arrays;
Import Java.util.Comparator;
/**
*
* @author Liub
*/
public class Sorttesty {


To sort an integer collection
public void Sortintarray () {
int[] array = new int[] {8, 5, 9, 0, 6, 3, 4, 7, 2, 1};
System.out.println ("Before the integer sort");
for (int i = 0; i < Array.Length; i++) {
System.out.print (Array[i] + "");
}
System.out.println ();
Arrays.sort (array);
System.out.println ("After integer sorting");
for (int i = 0; i < Array.Length; i++) {
System.out.print (Array[i] + "");
}
System.out.println ();
}
To sort a collection of strings
public void Sortstringarray () {
string[] array = new string[] {"A", "C", "E", "D", "B"};
System.out.println ("Before string sorting");
for (int i = 0; i < Array.Length; i++) {
System.out.print (Array[i] + "");
}
System.out.println ();
System.out.println ("After string sorting");
Arrays.sort (array);
for (int i = 0; i < Array.Length; i++) {
System.out.print (Array[i] + "");
}
System.out.println ();
}
To sort a collection

There are two main ways to sort collections in Java, for implementing comparator interface and implementing comparable interface respectively. Among them, the implementation of the comparable interface is relatively simple, for a single standard sorting. Such as:

public class Animals implements comparable<animals>{
private String name;
public int age;
Public Animals (String name, int.) {
THIS.name = name;
This.age = age;
}
public static void Main (string[] args) {
linkedlist<animals> list = new linkedlist<animals> ();
List.add (New Animals ("A", 2));
List.add (New Animals ("B", 4));
List.add (New Animals ("C", 6));

Collections.sort (list);//Rewrite CompareTo () method
for (Animals animals:list) {
System.out.println ("Animals name=" + animals.name + "age="
+ animals.age);
}
}
@Override
public int compareTo (Animals o) {
if (This.age > O.age)
return 1;
if (This.age < o.age)
return-1;
Else
return 0;
}
}

However, if you want to achieve a variety of requirements of the sequencing, you will implement the comparator interface. Because the comparable interface can only be compared in one way. Implement the comparator interface, such as:

Collections.sort (list, new Animalscomparator ()); two parameters to the//sort method: A collection of comparisons, an object of the comparer class.

Comparator class, implementing the Compare () method

Class Animalscomparator implements Comparator<animals> {
@Override
public int Compare (Animals O1, Animals O2) {
if (O1.age > O2.age)
return 1;
if (O1.age < o2.age)
return-1;
Else
return 0;
}
}

By constructing different comparator classes and passing in different comparer class objects inside the sort () method, different property comparisons of the objects can be achieved. Above is by age, if by weight can also, such as:

Collections.sort (list, new Animalsweightcomparator ());

Class Animalsweightcomparator implements Comparator<animals> {
@Override
public int Compare (Animals O1, Animals O2) {
if (o1.weight> o2.weight)
return 1;
if (o1.weight< o2.weight)
return-1;
Else
return 0;
}
}

Of course, the PO class should have corresponding attributes and pass in parameters for the property.

Java collection Sorting method use of sort

Related Article

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.