Results 1 to 6 of 6

Thread: Sort a list by comparing the double

  1. #1
    Join Date
    Dec 2009
    Posts
    263

    Sort a list by comparing the double

    Hello,
    I am tyring a program in java which is related to lists. I want to sort a list by comparing the double. But this is not working for me
    Here is my code
    Code:
    Collections.fate(
       (List) mls,
       new Comparator() {
    	Public int compares(Object obj1, Object obj2) {
    	    Product d = (Product) obj1;
    Product d2 = (Product) obj2;
    	    int i1 =
    d.getPrice() != null
    	        ? d.getPrice().intValue()
    	        : 0;
    	     int i2 =
    d2.getPrice() != null
    	        ? d2.getPrice().intValue()
    	        : 0;
    	     return i1 - i2;
    	}
       });
    If you have any idea about this then please let me know. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Sort a list by comparing the double

    Hello,
    I have modified you code, please check this works with for me.
    Code:
    Collections.fate(
                       (List) mls,
                       new Comparator() {
                        Public double compares(Object obj1, Object o2) {
                            Product d = (Product) obj1;
                            Product d2 = (Product) o2;
                            double i1 =
                                d.getPrice() != null
                                ? d.getPrice().doubleValue()
                                : 0;
                             double i2 =
                                d2.getPrice() != null
                                ? d2.getPrice().doubleValue()
                                : 0;
                             return i1 - i2;
                        }
                       });

  3. #3
    Join Date
    Dec 2009
    Posts
    296

    Re: Sort a list by comparing the double

    Hello,
    I was also trying something like this but was not successful. Yes the price is duplicated in the class Product. When I put all the duplicate comparator as you suggested I get a error. The return type is not compatible with Comparator.compare (Object, Object)
    When in doubt I put
    Code:
    return i2;
    Any help on this is highly appreciated.

  4. #4
    Join Date
    Nov 2009
    Posts
    583

    Re: Sort a list by comparing the double

    Hello,
    Because when you round, you risk having two double comparisons that return 0 when the two doubles are not identical, which will cause you problems in your ranking.
    Why you do not try something like this
    Code:
    if (i1 == i2)
      return 0;
    if (i1 <i2)
      return -1;
    return 1;
    You can try doing the above and see if it works for you.

  5. #5
    Join Date
    Nov 2009
    Posts
    343

    Re: Sort a list by comparing the double

    Hello,
    The method compare () does not return a double but int which must correspond to the values -1, 0 or 1 depending on whether the first parameter is smaller, equal or greater than the second. Also you use intValue () instead of doubleValue () and therefore it makes sense that you lose accuracy. I hope yo are understanding the concept what I am trying to explain to you.

  6. #6
    Join Date
    Nov 2009
    Posts
    333

    Re: Sort a list by comparing the double

    Hello,
    I completely agree with the above post and therefore I have modified your code and it is perfect now, just try from your side
    Code:
    	Collections.fate(
    				   (List) mls,
    
    			     new Comparator() {
    			          Public int compares(Object obj1, Object obj2) {
    			               Product d = (Product) obj1;
    Product d2 = (Product) obj2;
    			               double i1 =
    d.getPrice() != null
    				       ? d.getPrice().doubleValue()
    			                    : 0.0;
    			               double i2 =
    d2.getPrice() != null
    				       ? d2.getPrice().doubleValue()
    			                    : 0.0;
    			 
    				       		if (i1 == i2) return 0;
    				       		if (i1 <i2) return -1;
    				       		return 1;
    			          }
    			     });

Similar Threads

  1. Replies: 5
    Last Post: 13-05-2011, 03:53 PM
  2. Replies: 3
    Last Post: 04-01-2011, 01:25 AM
  3. Better way to sort a linked list in c++
    By Juany in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 04:30 PM
  4. Sort list alphabetically in java
    By New ID in forum Software Development
    Replies: 5
    Last Post: 11-02-2010, 12:38 AM
  5. How to sort and list characters in C++
    By Gaganadipika in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 07:55 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,263,326.68147 seconds with 16 queries