Sort the List in java

Source: Internet
Author: User

Mainly used: Collections. sort () method:

 

1. JavaBean -- Content. java:

  1. PackageCom. hmw. listsort;
  2. Public ClassContent {
  3. Private LongKey;
  4. PrivateString name;
  5. PublicContent (LongKey, String name ){
  6. This. Key = key;
  7. This. Name = name;
  8. }
  9. Public LongGetKey (){
  10. ReturnKey;
  11. }
  12. Public VoidSetKey (LongKey ){
  13. This. Key = key;
  14. }
  15. PublicString getName (){
  16. ReturnName;
  17. }
  18. Public VoidSetName (String name ){
  19. This. Name = name;
  20. }
  21. }

2. Comparison class -- ContentComparator. java:

  1. PackageCom. hmw. listsort;
  2. ImportJava. util. Comparator;
  3. Public ClassContentComparatorImplementsComparator <Content> {
  4. Public IntCompare (Content o1, Content o2 ){
  5. // Route null to the end
  6. If(O1 =Null){
  7. Return1;
  8. }
  9. If(O2 =Null|! (O2InstanceofContent )){
  10. Return-1;
  11. }
  12. LongKey1 = o1.getKey ();
  13. LongKey2 = o2.getKey ();
  14. ReturnKey1> key2? 1: key1 <key2? -1: 0;
  15. /*
  16. // If You Want To sort by name field, you only need to change the last three lines of code to the following line.
  17. Return o1.getName (). compareTo (o2.getName ());
  18. */
  19. }
  20. }

 

 

3. Test class -- CompareClient. java

  1. PackageCom. hmw. listsort;
  2. ImportJava. util. ArrayList;
  3. ImportJava. util. Collections;
  4. ImportJava. util. List;
  5. Public ClassCompareClient {
  6. Public Static VoidMain (String [] args ){
  7. List <content> List =NewArraylist <content> ();
  8. List. Add (Null);
  9. List. Add (NewContent (15000, "--- 15000 --"));
  10. List. Add (NewContent (10000, "--- 10000 ---"));
  11. List. Add (NewContent (20000, "--- 20000 ---"));
  12. List. Add (Null);
  13. List. Add (NewContent (25000, "--- 25000 ---"));
  14. List. add (NewContent (13000, "--- 13000 ---"));
  15. List. add (NewContent (15000, "--- 15000 ---"));
  16. List. add (NewContent (89000, "--- 89000 ---"));
  17. Collections. sort (list,NewContentComparator ());
  18. /*
  19. // Add this line of code in descending order.
  20. Collections. reverse (list );
  21. */
  22. For(Content content: list ){
  23. If(Content =Null){
  24. System. out. println ("null ");
  25. }Else{
  26. System. out. println ("content. getName ()/t" + content. getName ());
  27. }
  28. }
  29. }
  30. }

 

The output result is as follows:

Content. getName () --- 10000 ---
Content. getName () --- 13000 ---
Content. getName () --- 15000 --
Content. getName () --- 15000 ---
Content. getName () --- 20000 ---
Content. getName () --- 25000 ---
Content. getName () --- 89000 ---
Null
Null

 

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.