| Author |
How to sort array of string
|
deepak carter
Ranch Hand
Joined: Feb 19, 2011
Posts: 159
|
|
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
Joined: Aug 22, 2008
Posts: 191
|
|
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.
|
Do not wait to strike till the iron is hot; but make it hot by striking....
|
 |
Andi Eska
Greenhorn
Joined: Jun 20, 2003
Posts: 14
|
|
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
Joined: Feb 19, 2011
Posts: 159
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12952
|
|
|
Did you try what Andi Eska wrote?
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Manoj Kumar Jain
Ranch Hand
Joined: Aug 22, 2008
Posts: 191
|
|
Oops, My mistake didn't read
how to sort this without using sort method
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 6109
|
|
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
Joined: Jul 03, 2011
Posts: 256
|
|
|
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
Joined: Oct 13, 2005
Posts: 32830
|
|
|
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
Joined: Jul 03, 2011
Posts: 256
|
|
|
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
Joined: Mar 22, 2012
Posts: 11
|
|
|
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
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
Joined: Oct 30, 2012
Posts: 9
|
|
hi,
[solution removed]
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4901
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16811
|
|
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
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
 |
|
|
subject: How to sort array of string
|
|
|