import java.util.Arrays;
public class wrappertest {
public static void main(String[] args){
String strNum = "12 32 23 87 16";
System.out.println("old:"+strNum);
strNum = sortString(strNum);
System.out.println("new:"+strNum);
}
public static String sortString(String str){
//1.分割成字符串数组
String[] arr = str.split(" ");
//2.将其变为int数组
int[] a = new int[arr.length];
for (int i = 0; i < a.length; i++) {
a[i] = Integer.parseInt(arr[i]);
}
//3.对int排序
Arrays.sort(a);
//4.转化为字符串
String b = arrayToString(a);
return b;
}
public static String arrayToString(int[] arr_num){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr_num.length; i++) {
if(i!=arr_num.length-1){
sb.append(arr_num[i]+" ");
}
else{
sb.append(arr_num[i]);
}
}
return sb.toString();
}
}
结果:
old:12 32 23 87 16
new:12 16 23 32 87