• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I see if an array is already sorted?  RSS feed

 
Sergiu Dobozi
Ranch Hand
Posts: 107
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to see if an array already has sorted elements.
I tried a simple:


Also tried to do it with equals method and without a toString method but the fact is sort returns void so it cannot be compared. What to do in this case?
Thanks!
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a start, don't try the == operator on reference types like that. It doesn't work. Least of all when the two operands are of different types. And in the case of arrays, don't use the equals method like that; it isn't overridden and you would have to use this instead.

But there is a better way to find whether an array is sorted. You can write your own checking method:-Search my posts for details of utility classes for more explanation. I shall let you complete the method.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!