wood burning stoves*
The moose likes Java in General and the fly likes How to check StringArray is sorted or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to check StringArray is sorted or not?" Watch "How to check StringArray is sorted or not?" New topic
Author

How to check StringArray is sorted or not?

Vass Lee
Greenhorn

Joined: Nov 16, 2011
Posts: 8

Hi All,

I am new guy for JAVA. I want to check my String Array is sorted or not ? how can i achieve this ?.Any one give some sample ?



Here i want to check str is sorted or not ?.Values are stored in alphabetical ascending order or not ?

Thanks,
Vass Lee
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Vass Lee wrote:
Hi All,

I am new guy for JAVA. I want to check my String Array is sorted or not ? how can i achieve this ?.Any one give some sample ?



Here i want to check str is sorted or not ?.Values are stored in alphabetical ascending order or not ?

Thanks,
Vass Lee


You want to check or you actually want to sort?

To check you can simply print the array and see if the words are in alphabetical order.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Vass Lee
Greenhorn

Joined: Nov 16, 2011
Posts: 8
Hi Akhilesh Trivedi,


Thanks for your response.

I want to check pro grammatically the my string array is elements are sorted or not?

Actually i read some data from outside(some file or user given) and stored into String[].I don't change that order .what is the input order,same order i need to keep in String[].After storing the data i want to check that String[] elements are sorted or not ?If its not sorted i want give some message.

Thanks,
Vass Lee
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Firstly - how would you to it by yourself? How would you describe to someone else how to tell if a list of words was sorted?
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Take the given string array - array1.

Sort the string array using Arrays.sort() - array2
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Arrays.html#sort%28java.lang.Object[]%29]

Compare two arrays (array1 & array2)

If difference is found then there was a problem,
else it was already sorted.! !!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

There is a smarter way to check this than sorting the array yourself and comparing it to the original array. How do you think you could do this without sorting the array yourself?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Jesper de Jong wrote:There is a smarter way to check this than sorting the array yourself and comparing it to the original array. How do you think you could do this without sorting the array yourself?


How is that Jesper?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Akhilesh Trivedi wrote:How is that Jesper?

Well think about it: {"chanan","tapan","Amar","santosh","deepak"} - did you need to put that in alphabetical order to tell that it wasn't already in order? Or was there an easier way to tell?
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Matthew Brown wrote:
Akhilesh Trivedi wrote:How is that Jesper?

Well think about it: {"chanan","tapan","Amar","santosh","deepak"} - did you need to put that in alphabetical order to tell that it wasn't already in order? Or was there an easier way to tell?


I had to sort it. I can not think of any easier way... than to implement sorting like algorithm and do the check inside itself.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Compare the Strings in the array. If all of the Strings are greater than the preceding one return true; false otherwise.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Akhilesh Trivedi wrote:I had to sort it. I can not think of any easier way... than to implement sorting like algorithm and do the check inside itself.

If I gave you a thousand words in a random order, would you have to sort them all in order to tell that they weren't already sorted? I'm not talking about writing a program - I'm talking about doing it by sight.

(If you can sort a thousand words that quickly in your head, I'm impressed!
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Matthew Brown wrote:
Akhilesh Trivedi wrote:I had to sort it. I can not think of any easier way... than to implement sorting like algorithm and do the check inside itself.

If I game you a thousand words in a random order, would you have to sort them all in order to tell that they weren't already sorted? I'm not talking about writing a program - I'm talking about doing it by sight.

(If you can sort a thousand words that quickly in your head, I'm impressed!


And I'm impressed too.

you only need to see through once.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to check StringArray is sorted or not?
 
Similar Threads
spaces in String
Passing a Variable value from one Method to another Method
How to check StringArray is sorted or not?
How to ignore case in Set ?
FileFilter?