Results 1 to 9 of 9
  1. #1
    BlueBird is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default 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 :)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Sorting in string

    Do you want to sort the words of a sentence according to the length of the word, isn't it?

  3. #3
    BlueBird is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default Re: Sorting in string

    Yes, but there's a condition that follows which i've mentioned above..

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Sorting in string

    Split the sentence by space. Add them into a array list and sort. That is all you have do.

  5. #5
    BlueBird is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default 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 :/

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default 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.


  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,196
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    BlueBird is offline Member
    Join Date
    Mar 2011
    Posts
    9
    Rep Power
    0

    Default 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.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Sorting a String array based on a substring
    By jonytek in forum New To Java
    Replies: 1
    Last Post: 06-07-2011, 06:21 AM
  2. Scanner, while loop and sorting arrays/string?
    By RSYR in forum New To Java
    Replies: 10
    Last Post: 04-20-2011, 06:13 PM
  3. Sorting string Array Values
    By Zerpol in forum New To Java
    Replies: 1
    Last Post: 01-14-2011, 07:08 AM
  4. Help in sorting
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 01-08-2011, 06:15 PM
  5. sorting
    By jot321 in forum New To Java
    Replies: 18
    Last Post: 10-02-2008, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •