简体   繁体   中英

Is there a way to sort a String in java alphabetically without putting the String into an Array?

In the code below, I tried to compare char at i with the char at i+1. My understanding is that by using charAt(): I can take the character from the string and treat it as integer and be able to compare two characters. This part of the code works, but I think I am missing something in the code, hence it is not printing the desired result. Unless this way of sorting characters in a String is not valid.

public class stringAlphabetical {

    public static void main(String[] args){
        String word="watch";
        boolean swapped;
        char temp = ' ';
        do{
            swapped = false;
            for(int i=0;i<word.length()-1;i++){
                char a = word.charAt(i);
                char b = word.charAt(i+1);

                if(word.charAt(i)>word.charAt(i+1)){   // if (a>b) {
                   temp = a;
                   a = b;
                   b = temp;
                }
            }


        }while (swapped==true);

        System.out.println(word);
    }
}

Java String is immutable , so you will need to use a mutable class (like StringBuilder ) - (also, you are modifying char values, not a reference) and you don't need t .

StringBuilder word = new StringBuilder("watch");
boolean swapped;
do {
    swapped = false;
    for (int i = 0; i < word.length() - 1; i++) {
        char a = word.charAt(i), b = word.charAt(i + 1);

        if (a > b) { // <-- this is fine.
            word.setCharAt(i, b);
            word.setCharAt(i + 1, a);
            swapped = true;
        }
    }
} while (swapped);
System.out.println(word);

Which outputs

atchw

Or just use an array (for the same result)

String word = "watch";
char[] c = word.toCharArray();
Arrays.sort(c);
System.out.println(new String(c));

Use this code for sort the String array via alphabetical order without storing in any array

    Scanner kbd = new Scanner(System.in);
    String input = kbd.nextLine();
    String sortedString = Stream.of(input.split("")).sorted().collect(Collectors.joining());
    System.out.print(sortedString);

To sort the string alphabetically, you need to compare each character with all the characters and if the condition satisfies then you swap the character. This is shown using multiple loops. Lastly I print the char array.

public static void main(String[] args){
        String watchString = "watch";
        int j;
        char temp;

        char[] chars = watchString.toCharArray();

        for (int i = 0; i <chars.length; i++) {

            for ( j = 0; j < chars.length; j++) {

                if(chars[j]>chars[i]){
                    temp=chars[i];
                    chars[i]=chars[j];
                    chars[j]=temp;
                }

            }

        }

        for(int k=0;k<chars.length;k++){
            System.out.print(chars[k]);
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM