Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to sort array of string

 
deepak carter
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • 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



 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • 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.
 
Andi Eska
Greenhorn
Posts: 14
BSD Eclipse IDE VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15281
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try what Andi Eska wrote?
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, My mistake didn't read

how to sort this without using sort method
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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".
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • 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...
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • 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
  • 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")
 
Arian Gerryts
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
 
Venkat basa
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

[solution removed]
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic