Q: Java how to rearrange numbers inside a String String str1 = "11,22,13,31,21,12";
String str2 = "11,12,13,21,22,31";

Is there a simple clean way to modify str1 to get str2?

A: A Java 8 solution with a stream:
String str2 = Stream
    .of(str.split(","))
    .map(Integer::parseInt)
    .sorted()
    .map(String::valueOf)
    .collect(joining(","));

Note: For the joining method I have a import static java.util.stream.Collectors.joining.
The interpretation as a number (Integer::parseInt) is important, as this will correctly sort the input.

A: If I understood your question correctly then I think you wanted to rearrange in ascending order
public class test {
    public static void main(String[] args) {
        String str = "11,22,13,31,21,12";String str1[]=str.split(",");

        Arrays.sort(str1);
        for(String s:str1)
        {
            System.out.print(s+" ");
        }
    }

output: 11 12 13 21 22 31 

A: Check below code and comment:
import java.util.Arrays;

public class SortString {
    public static void main(String[] args) {
        String str = "11,22,13,31,21,12";
        StringBuffer strB = new StringBuffer();
        String[] arr = str.split(","); // Split string save in array
        Arrays.sort(arr); // sort array
        for (int i = 0; i < arr.length; i++) {  //add array in stringbuffer
            if (i != arr.length - 1) {
                strB.append(arr[i] + ","); 
            } else {
                strB.append(arr[i]);
            }
        }

        String str2 = strB.toString();
        System.out.println(str2);
    }
}


A: Try this simple snippet, gives exactly what you've asked :-
    String[] splitData;
    String str1 = "11,22,13,31,21,12";
    System.out.println("str1 : "+str1);
    String str2 = "11,12,13,21,22,31";
    splitData =  str1.split(",");
    Arrays.sort(splitData);

    System.out.print("str2 : ");
    for(int i=0; i<splitData.length; i++){
        if(i!=(splitData.length-1))
            System.out.print(splitData[i]+",");
        else
            System.out.print(splitData[i]);
    }

Output:
run:
str1 : 11,22,13,31,21,12
str2 : 11,12,13,21,22,31
