• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparator and sort sequences

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,

I have a doubt related to sort sequences like how this sort(<list>,<comparator> method is able to handle to display the result in ascending lexicographic order and then descending order after making a change in call to compareTo in terms of order of references to Objects.

like
public int compare(Object s1,Object s2)
{
return ((String)s1).compareTo((String)s2);
}

results in increasing order while

return ((String)s2).compareTo((String)s1);

will result in decreasing order.
Even though the first as well as second gives a negative or positive or zero value ,then how it recognizes whether it has to be in increasing order or decreasing order.

I hope , i have made my doubt clear

Please Clarify it...i am not able to get it how it really works


import java.io.*;
import java.util.*;
public class Delete
{

public static void main(String...s)
{
ArrayList al = new ArrayList();
al.add("canada");
al.add("bhutan");
al.add("afghan");
System.out.println("Before sorting "+al);
Collections.sort(al);
System.out.println("After sorting and abt to sort using comparator "+al);
RevSort rs=new RevSort();
Collections.sort(al,rs);
System.out.println("After comparator creation "+al);

}

static class RevSort implements Comparator
{
public int compare(Object s1,Object s2)
{
return ((String)s2).compareTo((String)s1);
}
}
}
output is
*****************

Before sorting [canada, bhutan, afghan]
After sorting and abt to sort using comparator [afghan, bhutan, canada]
After comparator creation [canada, bhutan, afghan]
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public int compare(Object s1,Object s2)
{
return ((String)s1).compareTo((String)s2);
}

results in increasing order while

return ((String)s2).compareTo((String)s1);

Suppose ,
String s1 is smaller than s2 according to dictionary order.
((String)s1).compareTo((String)s2) will return negative no.
so method public int compare(Object s1,Object s2) will return negative no. . It shows s1 is smaller than s2 .

whereas

((String)s2).compareTo((String)s1) will return positive no.
so method public int compare(Object s1,Object s2) will return positive no. . It shows that s1 is greater than s2 .

That is the reason second one arranges objects in descending order .

 
Jolly Tiwari
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Buddy that's what i wanna say ,How can one decide the kind of sequence (whether ascending or descending ) just by looking at the sign of compareTo() .even we would get these positive and negative values for comparisons of different values finally resulting in ascending order(or even in descending order)

Hope i am able to explain my doubt
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic