Sort a single String in Java

后端 未结 10 940
误落风尘
误落风尘 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:57

        String a ="dgfa";
        char [] c = a.toCharArray();
        Arrays.sort(c);
        return new String(c);
    

    Note that this will not work as expected if it is a mixed case String (It'll put uppercase before lowercase). You can pass a comparator to the Sort method to change that.

提交回复
热议问题