File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reverse Sort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reverse Sort " Watch "Reverse Sort " New topic
Author

Reverse Sort

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

How to achieve reverse ordering using comparable ?


Tell the difficulties that i am difficult.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

saloni jhanwar wrote:How to achieve reverse ordering using comparable ?


i think above code does not make sense in what you are asking Saloni. because your class PrQu is implementing comparable interface whereas you are adding Integers to your priority queue. so in this case the priority queue will be ordered according to the natural ordering of its elements which are Integers in your case. had you were adding PrQu objects to your priority queue then above code would have valid. wrapper classes implements comparable interface already. if you want to sort above priority queue in reverse order you need to supply it the comparator. it wont be possible by comparable interface.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

gurpeet singh wrote:
saloni jhanwar wrote:How to achieve reverse ordering using comparable ?


i think above code does not make sense in what you are asking Saloni. because your class PrQu is implementing comparable interface whereas you are adding Integers to your priority queue. so in this case the priority queue will be ordered according to the natural ordering of its elements which are Integers in your case. had you were adding PrQu objects to your priority queue then above code would have valid. wrapper classes implements comparable interface already. if you want to sort above priority queue in reverse order you need to supply it the comparator. it wont be possible by comparable interface.


i don't care it makes sense or not i am just experimenting.As we do swapping of objects in comparator then why cant in comparable ? I will try with PrQu objects.
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

An unbounded priority queue based on a priority heap. This queue orders elements according to an order specified at construction time, which is specified either according to their natural order (see Comparable), or according to a Comparator, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

More info here


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Sumit Patil wrote:An unbounded priority queue based on a priority heap. This queue orders elements according to an order specified at construction time, which is specified either according to their natural order (see Comparable), or according to a Comparator, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

More info here



public interface Comparable<T>

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.


Ok nice.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Sumit Patil wrote:An unbounded priority queue based on a priority heap. This queue orders elements according to an order specified at construction time, which is specified either according to their natural order (see Comparable), or according to a Comparator, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

More info here


did you mean mutual objects only ?
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

saloni jhanwar wrote:
did you mean mutual objects only ?

I am sorry, but i am not sure what you mean by mutual objects.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

saloni jhanwar wrote:How to achieve reverse ordering using comparable ?






this is how you will do reverse sort using comparable.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Sumit Patil wrote:
saloni jhanwar wrote:
did you mean mutual objects only ?

I am sorry, but i am not sure what you mean by mutual objects.




at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)
at PrQu.compareTo(PrQu.java:16)

???
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

The error here is
Exception in thread "main" java.lang.StackOverflowError.

This is happening because the compareTo method is called again and again until the stack explodes. What i think is,you should use some property of pr object in the compareTo method.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Sumit Patil wrote:The error here is
Exception in thread "main" java.lang.StackOverflowError.

This is happening because the compareTo method is called again and again until the stack explodes. What i think is,you should use some property of pr object in the compareTo method.


I thought how it will work without property anyways i want to achieve reverse ordering with comparable now it is going on




F:\OCJP\Chapter7\PriorityQueueClass>java PrQu
d
c
b
a

dcba
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Great !!!

The same can be done using Comparator as

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reverse Sort