0

I'm trying to code a program that will sort the given strings one by one first and then sorting all the strings both in decreasing order I managed to sort the string character by character but I am having trouble in sorting all the sorted (character by character) strings. I tried using the Array.sort() but it does not sort it decreasingly and it only sorts the first input not the already sorted array

    package com.company;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static void sortString(String str)
    {
        char[] chArr = str.toCharArray();
        String SortString = "";
        for (int i = 0; i< chArr.length; i++)
        {
            for (int j = 0; j< chArr.length; j++)
            {
                if(chArr[i] > chArr[j])
                {
                    char temp = chArr[i];
                    chArr[i] = chArr[j];
                    chArr[j] = temp;
                }
            }
        }
        String[] SortedString = new String[5];

        for (int k = 0; k<chArr.length;k++)
        {
            SortString = SortString + chArr[k];
        }
        Arrays.sort(SortedString);
        for (int counter = 0; counter<5; counter++)
        {
            System.out.println(SortedString[counter]);
        }


    }
    public static void main(String[] args)
    {
        Scanner UserInput = new Scanner (System.in);

        String[] names = new String[5];

        for (int counter = 0; counter<5; counter++)
        {
            do
            {
                System.out.print("Input String #" + (counter+1) + ": ") ;
                names[counter] = UserInput.next().toLowerCase();
            }while(names[counter].length() > 25);

        }
        UserInput.close();
        Arrays.sort(names);
        for (int counter = 0; counter<5; counter++)
        {
            sortString(names[counter]);
        }


    }

}
1
  • Did you try using Collections.reverseOrder()? Commented Aug 23, 2020 at 8:57

1 Answer 1

1
static String sortString(String str)
{
    char[] chArr = str.toCharArray();
    for (int i = 0; i< chArr.length; i++)
    {
        for (int j = 0; j< chArr.length; j++)
        {
            if(chArr[i] > chArr[j])
            {
                char temp = chArr[i];
                chArr[i] = chArr[j];
                chArr[j] = temp;
            }
        }
    }
    
    return new String(chArr);
}

public static void main(String[] args)
{
    Scanner UserInput = new Scanner (System.in);

    String[] names = new String[5];

    for (int counter = 0; counter<5; counter++)
    {
        do
        {
            System.out.print("Input String #" + (counter+1) + ": ") ;
            names[counter] = UserInput.next().toLowerCase();
        }while(names[counter].length() > 25);

    }
    UserInput.close();
    // Arrays.sort(names);   No point sorting here
    
    String[] strings = new String[5];
    for (int counter = 0; counter<5; counter++)
    {
        strings[counter] = sortString(names[counter]);
    }

     Arrays.sort(strings);

      // increasing order:
     for(String s : strings) {
         System.out.println(s);
     }
     
     // decreasing order:        
     for(int i = 4; i >= 0; i--) {
         System.out.println(strings[i]);
     }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thankyou for your answer I tried to compile it but it is not Sorting each individual string anymore
It will if you paste into your Main class.
Input: Input Inputz Java Javax Python Output:pthony inptuz inptu ajavx ajav Expected Output: zutpni xvjaa ytponh vjaa utpni
NeverMind I got it by using the last part of your code THANKYOU SO MUCH YOU ARE A LIFE SAVER
Please add some explanations. Just dropping code only answers isn't a good thing. People learn by understanding, and that works much better with context information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.