Use Collections. sort () to sort List/ArrayList

Source: Internet
Author: User

I wrote an Android app today and encountered a problem of sorting the List. After a google prompt, I found that Collections was used. sort (List list, Comparator c) can sort List/ArrayList. It is very Happy to write code for testing. The result shows that sorting does not work. Check it, no problem with the Code (the Code is as follows). It's strange.

 
 
  1. Public void changeSort (int type ){
  2. Comparator <ItemBean> comparator;
  3. Toast. makeText (this, "type =" + type, 5). show ();
  4. Switch (type ){
  5. Case 1 :{
  6. // Sort prices from low to high
  7. Comparator = new PriceLowToHighComparator ();
  8. Collections. sort (list, comparator );
  9. }
  10. Break;
  11. Case 2 :{
  12. // Sort prices from high to low
  13. Comparator = new PriceHighToLowComparator ();
  14. Collections. sort (list, comparator );
  15. }
  16. Break;
  17. }
  18. Adapter. setItemBeanList (list );
  19. Adapter. notifyDataSetChanged ();
  20. }

 
 
  1. public class PriceHighToLowComparator implements Comparator<ItemBean> { 
  2.  
  3.     @Override 
  4.     public int compare(ItemBean itemBean1, ItemBean itemBean2) { 
  5.          
  6.         double price1 = 0; 
  7.         double price2 = 0; 
  8.          
  9.         if(itemBean1.getItemPrice() != null && !"".equals(itemBean1.getItemPrice())){ 
  10.             price1 = Double.parseDouble(itemBean1.getItemPrice()); 
  11.         } 
  12.          
  13.         if(itemBean2.getItemPrice() != null && !"".equals(itemBean2.getItemPrice())){ 
  14.             price2 = Double.parseDouble(itemBean2.getItemPrice()); 
  15.         } 
  16.          
  17.         if(price1 > price2){ 
  18.             return -1; 
  19.         } 
  20.         else if(price1 < price2){ 
  21.             return 1; 
  22.         } 
  23.         else{ 
  24.             return 0; 
  25.         } 
  26.     } 
  27.  

Then there are various changes to the return value. After three retries, the problem is found. The returned value is incorrect. The above class is the list price from high to low rule class, Collections. sort (List list, Comparator c) sorts the data in the list based on the returned values. If price1> price2 at first, return 1, but the test shows that the data in the list is not changed, then change to return-1. The experiment found that the data in the list can be sorted according to the expected results, but the new problem arises. Why is price1> price2 but-1 returned?

Continue to google, found that the master of the http://solodu.iteye.com/blog/630891 article at the end of the article said a paragraph "to fully understand the order must first understand the last return 0,-1, 1. The meaning of these three numbers. They do not represent numbers, but the values to be compared between the front and back. Which one is big or small? If it is 0, the two numbers to be compared are equal. If it is 1, it indicates that the preceding number is large. If it is-1, it indicates that the preceding number is small. It's okay to understand this ." However, in combination with the sorting rule I wrote, I suddenly felt that the numbers 0, 1,-1 do not seem to mean who is big or small, but a sorting problem. When-1 is returned, the preceding variable (price1) is in front of the variable price2), and vice versa. If so, just as there are many sorting requirements for this project, I will write a few more rules to test the results. According to different rules, if you want to put the preceding variable before the variable,-1 is returned.

 

This article from the "free between Android and ios" blog, please be sure to keep this source http://jerrysun.blog.51cto.com/745955/608416

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.