Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reverse Sort

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to achieve reverse ordering using comparable ?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
saloni jhanwar wrote:How to achieve reverse ordering using comparable ?






this is how you will do reverse sort using comparable.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great !!!

The same can be done using Comparator as

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic