About customizing the list collection ordering Method!

Source: Internet
Author: User

Approximate process:

Sorting is used to sort the interface comparator<t>
You first have to build a class to implement the comparator comparator

Approximate process
public class Stucomp implements comparator<student> {public int compare (Student O1, Student O2) {return o1.getname () . Comparetoignorecase (O2.getname ());}}

Then add this sort comparer to the collections.sort (list);
Collections.sort (List,new Stucomp ()); The result is right.

Example: There is an implementation class about the message, depending on the time of the message from the large row to the small, depending on whether the message has been read before the unread row, read the shot

The implementation class for the message:

 PackageCom.educationcrm.model;/*** Created by Laobiao on 2016/5/21. * Message List Object*/ Public classNewslistmodel {Private intNewsId;//Message ID    PrivateString Sendname;//Sender's name    PrivateString title;//the title of the message    PrivateString Day;//Message Send Time    PrivateString Idread;//whether it has been read    PrivateString type;//Message Type     PublicNewslistmodel (intnewsId, String sendname, string title, String day, String idread, String type) {NewsId=newsId;  This. Sendname =Sendname;  This. title =title;  This. Day =Day ;  This. Idread =Idread;  This. Type =type; }     Public intGetnewsid () {returnNewsId; }     Public voidSetnewsid (intnewsId) {NewsId=newsId; }     PublicString Getsendname () {returnSendname; }     Public voidsetsendname (String sendname) { This. Sendname =Sendname; }     PublicString GetTitle () {returntitle; }     Public voidSettitle (String title) { This. title =title; }     PublicString GetDay () {returnDay ; }     Public voidSetday (String day) { This. Day =Day ; }     PublicString Getidread () {returnIdread; }     Public voidSetidread (String idread) { This. Idread =Idread; }     PublicString GetType () {returntype; }     Public voidsetType (String type) { This. Type =type; }}

Here are two sorting methods;

//Date ComparisonclassDaycomparatorImplementsComparator<newslistmodel>{@Override Public intCompare (Newslistmodel O1, Newslistmodel O2) {if(o1==NULL){           return-1; }        if(o2==NULL){            return-1; } SimpleDateFormat Sim=NewSimpleDateFormat ("MM month DD Day hh:mm"); Try {            LongO1date=Sim.parse (O1.getday ()). GetTime (); LongO2date=Sim.parse (O2.getday ()). GetTime (); if(o1date>o2date) {                return-1; }Else {                return1; }        } Catch(ParseException e) {e.printstacktrace (); }        return0; }}//whether read comparisons are made by judging their read properties to return a valueclassIsreadcomparatorImplementsComparator<newslistmodel>{@Override Public intCompare (Newslistmodel O1, Newslistmodel O2) {if(o1==NULL){            return-1; }        if(o2==NULL){            return1; }        if(O1.getidread () = = "Yes" && o2.getidread () = = "No"){            return1; }        if(O1.getidread () = = "No" && o2.getidread () = = "Yes"){            return-1; }        return0; }

Here's how it's implemented:

Collections.sort (A,new  daycomparator ());//First Date sort Collections.sort (A,new Isreadcomparator ());//re-order the time

Classes and methods to use:

Sort Tool Class:comparator<t>;

The method of sorting judgment: compare; The return value is 1 o'clock, the second parameter is preceded by the first argument, 1 is the first argument, and 0 o'clock is not;

Call Sort method: Sort (<T>object,Comparator<T> x);

  

Create a new Comparator<t> implementation class, override the Compare () method in the class, and then use the List.sort () method to invoke the implementation class, which can be sorted;

About customizing the list collection ordering Method!

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.