Generic collection ordering, sorted by an attribute of an entity class object

Source: Internet
Author: User

Generic collection ordering, sorted by an attribute of an entity class object

Import Java.util.arraylist;import java.util.collections;import Java.util.comparator;import Java.util.List;import Java.lang.reflect.method;import Java.lang.reflect.invocationtargetexception;import sy.model.aite.analysis.selftestanalysis;/** * Generic collection ordering, sorted by an attribute of an entity class object * @author Admin * * @param <E> */public class so rtlist<e> {public void sort (list<e> list, final string method, final string sort) {Collections.sort (list, new Comparator<object> () {public int compare (object A, object b) {int ret = 0;try {Method m1 = ((E) a). GetClass (). Getmet Hod (method, NULL); Method m2 = ((E) b). GetClass (). GetMethod (method, NULL), if (sort! = null && "desc". Equals (sort))//REVERSE RET = M2.invok E ((((e) b), null). ToString (). CompareTo (M1.invoke (((e) a), null). ToString ()), else//positive-order ret = M1.invoke ((((e) a), null). ToString (). CompareTo (M2.invoke (((E) b), null). ToString ()); catch (Nosuchmethodexception ne) {System.out.println (NE);} catch (Illegalaccessexception IE) {System.out.println (IE);} CatcH (invocationtargetexception it) {System.out.println (it);} return ret;});} public static void Main (string[] args) {list<selftestanalysis> List = new arraylist<selftestanalysis> (); Selftestanalysis st1 = new Selftestanalysis () st1.setstatus ("OK"); St1.setplatenumber ("license plate"); St1.setforsort (2); List.add (ST1); Selftestanalysis st2 = new Selftestanalysis () st2.setstatus ("NG"); St1.setplatenumber ("license plate"); St2.setforsort (3); List.add (ST2); Selftestanalysis st3 = new Selftestanalysis () st3.setstatus ("OFF"); St1.setplatenumber ("license plate"); St3.setforsort (1); List.add (ST3); Selftestanalysis st4 = new Selftestanalysis () st4.setstatus ("OFF"); St1.setplatenumber ("license plate"); St4.setforsort (1); List.add (ST4); SYSTEM.OUT.PRINTLN ("default sort"); for (Selftestanalysis test:list) {System.out.println (test.tostring ());}        System.out.println ();        sortlist<selftestanalysis> sort = new sortlist<selftestanalysis> ();        System.out.println ("Getnum in desc order"); Sort.        Sort (list, "Getforsort", "desc"); for (selftestAnalysis Test:list) {System.out.println (test.tostring ());}                System.out.println ();        SYSTEM.OUT.PRINTLN ("getnum asc sort"); Sort.        Sort (list, "GetStatus", null); for (Selftestanalysis test:list) {System.out.println (test.tostring ());}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Generic collection ordering, sorted by an attribute of an entity class object

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.