The Collections.sort method of Android Development sort list by time

Source: Internet
Author: User
Tags comparable

Compare (A,b) method: Returns a negative integer, 0, or positive integer, respectively, based on the first argument being less than, equal to, or greater than the second argument.
Equals (obj) method: Returns True only if the specified object is also a Comparator and enforces the same sort as this Comparator.
Collections.sort (list, new Pricecomparator ()); The second argument returns a value of int, which is equivalent to a flag that tells the sort method to sort the list in what order.

have been no contact sort, suddenly want to achieve the list of sort, looked for information, found Collections.sort () this method.

Collections.sort () is divided into two parts, one part is collation, and the other is sorting algorithm.

Rules are used to judge objects, and algorithms consider how to sort them.

For custom objects, sort () does not know the rule, so it cannot be compared, so it is important to define the collation. There are two kinds of ways:

First, Java.lang the following interface: comparable. Allows a custom object to implement a comparable interface with only one method Comparableto (object o)

The rule is that the current object is compared to the O object and returns an int value, sorted by the system.

If the current object >o the object, the return value >0;

If the current object =o object, the return value = 0;

If the current object <o the object, the return value <0;

The second way, Java.util has a comparator (comparator). It has a compare () method to compare two objects.

So far, I have adopted a second approach. The following are the collation rules defined:


public class Jsoncomparator implements comparator&lt; Newrecordentity&gt; {

@Override
public int Compare (newrecordentity arg0, newrecordentity arg1) {

String data1=timestamp2date (Arg0.getoldtime () + "", "Yyyy-mm-dd HH:mm:ss");
String data2=timestamp2date (Arg1.getoldtime () + "", "Yyyy-mm-dd HH:mm:ss");
Set the time template
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");
Time to be given a model
try {
Date D1 = sdf.parse (data1);
Date D2 = Sdf.parse (DATA2);
if (D1.gettime ()-d2.gettime () &gt;0) {
return-1;
}else if (D1.gettime ()-d2.gettime () &lt;0) {
return 1;
}


catch (ParseException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}


return 0;
}
public string Timestamp2date (string timestampstring, string formats) {
Long timestamp = Long.parselong (timestampstring) *1000;
String date = new Java.text.SimpleDateFormat (formats). Format (new java.util.Date (timestamp));
return date;
}
}

Use:

Sort operations
Jsoncomparator pcomparator = new Jsoncomparator ();
Collections.sort (list, pcomparator);

This enables the sorting function. If the array is sorted, the same principle is only replaced by the Arrays.sort () method.

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.