Results 1 to 9 of 9
Thread: Sorting in string
- 02-22-2012, 07:32 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
Sorting in string
I have to write a code for sorting a sentence according to its length. If the length of two or more words are equal I then have to print the words like they were in the original string.
Like-the sky is blue.
Output- is the sky blue
I applied a simple bubble sort but that sorts the words with the same length in an alphabetical order and is giving me the output- "is sky the blue".
There needs to be another condition with can compare the indexes of the words of the same length and then maybe compare them to the original string.
I just can't get what the code should be.
My code:
I first used the string tokenizer and saved the words in an array. then,
for(int i=0;i<c-1;i++)
{
for(int j=i+1;j<c;j++)
{
if(a[i].length()>a[j].length())
{
String t=a[i];
a[i]=a[j];
a[j]=t;
}
else if(a[i].length()==a[j].length())
{
//I don't know what i should write here :P
}
}
}
Help will be really appreciated :)
- 02-22-2012, 07:34 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Sorting in string
Do you want to sort the words of a sentence according to the length of the word, isn't it?
- 02-22-2012, 07:43 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
Re: Sorting in string
Yes, but there's a condition that follows which i've mentioned above..
- 02-22-2012, 07:49 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Sorting in string
Split the sentence by space. Add them into a array list and sort. That is all you have do.
- 02-22-2012, 07:53 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
Re: Sorting in string
Yes I did that. But I don't want the words with the same length to be sorted alphabetically. I need them to be placed the same way they were in the original string.
Input- the lines are printed in reverse order
Output- in the are lines order printed reverse
On applying a simple sort acc. to the length my output is --> in are the lines printed reverse which is not the output which i want :/
- 02-22-2012, 09:11 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Sorting in string
So you have to keep the index too. According to your example "the lines are printed in reverse order", words "the" and "are" in same length and you want to keep them in the same order. So before sort the list you can extract that segment and place it accordingly.
- 02-22-2012, 09:36 AM #7
Re: Sorting in string
Prepare a stack of blank cards, or you could even use post-it notes. Write one word of the sentence on each. Arrange them on a table to form the sentence.
Then sort them manually, according to the terms of the assignment. Once you can do that systematically, transform your logical steps into code.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-22-2012, 05:58 PM #8
Member
- Join Date
- Mar 2011
- Posts
- 9
- Rep Power
- 0
Re: Sorting in string
Even if i save the index positions when the length is equal. I just don't know how on earth should i put it in my sorted array. I'll have to search my sorted array for those words
then swap them with the ones whose indexes i've saved. I don't know. This is killing me. :( Nothing makes sense.
- 02-28-2012, 10:10 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Re: Sorting in string
Did you solve this out. I couldn't logged into the forum last few days.
Similar Threads
-
Sorting a String array based on a substring
By jonytek in forum New To JavaReplies: 1Last Post: 06-07-2011, 06:21 AM -
Scanner, while loop and sorting arrays/string?
By RSYR in forum New To JavaReplies: 10Last Post: 04-20-2011, 06:13 PM -
Sorting string Array Values
By Zerpol in forum New To JavaReplies: 1Last Post: 01-14-2011, 07:08 AM -
Help in sorting
By nn12 in forum New To JavaReplies: 3Last Post: 01-08-2011, 06:15 PM -
sorting
By jot321 in forum New To JavaReplies: 18Last Post: 10-02-2008, 10:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks