• 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

How to check StringArray is sorted or not?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Vass Lee
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.! !!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Akhilesh Trivedi
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compare the Strings in the array. If all of the Strings are greater than the preceding one return true; false otherwise.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic