Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How to sort array of string

 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Can anyone help me in sorting an array of string

suppose i have this

String s="String"

This i did by coverting it to character array and then sorted it (if there is any other approach please do let me know)

But i am not having any luck for this type like

String s={"tcs","wipro","cognizant"}

how to sort this without using sort method



 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This i did by coverting it to character array and then sorted it (if there is any other approach please do let me know)


You meant you sorted a String's character alphabetically means "string" is converted to "ginrst".
the same you want to apply at the array of the String. so you can iterate over the array and can apply the same logic for each string in the Array.

If you are talking about the sorting of the Stings in the Array without changing them (reshuffling of the strings itself not characters) then you can use sort method of Array.
 
Greenhorn
Posts: 14
Eclipse IDE VI Editor BSD
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepak carter wrote:
String s={"tcs","wipro","cognizant"}

how to sort this without using sort method



Which sort Method do you mean? Arrays.sort() ?

The easiest way is to use Arrays.sort().

String[] s={"tcs","wipro","cognizant"}
java.util.Arrays.sort(s);



If that's not an option: you may have a look at BubbleSort site:code.google.com Jeff Heaton

 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

Thanks for the reply

What i want to do is

suppose i have string like this

String[] s={"c","b","a"}

and sorted array should be

a b c

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try what Andi Eska wrote?
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, My mistake didn't read

how to sort this without using sort method

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

deepak carter wrote:
how to sort this without using sort method



Do you mean "without using any sort() method provided by the core API?"

If so, then just write your own sort() method. And before you ask, "How do I do that?" the answer is that you start by figuring out and writing down the basic, simple steps you would use to do it "manually".
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deepak, every character in Java has an underlying integral representation. I suggest you to use this information in coming up with your logic...
 
Marshal
Posts: 74359
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don’t need to look at chars; String implements Comparable<String> and there is a Comparator<String> available as a static field of the String class. You can use those instead.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes that is perhaps a better way to go about it, you'll have to execute a lot more steps in case there is no difference in characters.(E.g. : "aaa", "aab", "aac")
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arian Gerryts wrote:


why this switching array to list. Again Collections.sort will do a switch from list to array and then it use again Arrays.sort
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

[solution removed]
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Venkat basa wrote:hi...


Hi Venkat, and welcome to JavaRanch,

I'm sure your intentions were great, but we don't like people to post ready-made solutions, especially in the Beginners forum. Much better to provide tips (eg, a link to sort algorithms) and let OP work it out for him/herself. I've kept a copy of your code and will restore it if my colleagues suggest I should.

Also: if you do post code, please read the UseCodeTags (←click) page first.

I also removed your duplicate post.

Winston
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:

Arian Gerryts wrote:


why this switching array to list. Again Collections.sort will do a switch from list to array and then it use again Arrays.sort



I think that it is safe to assume that, with a requirement of "without using sort method", that this is likely a homework problem -- and any core package that does sorting will not be allowed.

But a question for the OP, can you show us what you done so far? We can't give any hints in the right direction, if we don't know what you done so far.

Henry
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic