Sort a single String in Java

后端 未结 10 936
误落风尘
误落风尘 2020-11-28 18:15

Is there a native way to sort a String by its contents in java? E.g.

String s = \"edcba\"  ->  \"abcde\"
10条回答
  •  一向
    一向 (楼主)
    2020-11-28 18:40

    A more raw approach without using sort Arrays.sort method. This is using insertion sort.

    public static void main(String[] args){
        String wordSt="watch";
        char[] word=wordSt.toCharArray();
    
        for(int i=0;i<(word.length-1);i++){
            for(int j=i+1;j>0;j--){
                if(word[j]

提交回复
热议问题