原文:分享1个很实用的java代码,可以将以逗号分隔的数字以从大到小的方式排序后返回
源代码下载地址:http://www.zuidaima.com/share/1550463693032448.htm
对web开发中,多个id的排序很实用,有需要的牛人可以下载。
package com.zuidaima.math;
import java.util.Arrays;
/**
*@author www.zuidaima.com
**/
public class SortIDString {
public static void main(String[] args) {
String user_ids = "1,3,2,299,33,2";
String[] _ids = user_ids.split(",");
System.out.println("排序前:"
+ Arrays.toString(_ids).replaceAll("[|]", ""));
for (int i = 0; i < _ids.length; i++) {
int largest = i;
for (int j = i; j < _ids.length; j++) {
if (Integer.parseInt(_ids[j]) > Integer.parseInt(_ids[largest]))
largest = j;
}
int temp;
temp = Integer.parseInt(_ids[i]);
_ids[i] = _ids[largest];
_ids[largest] = temp + "";
}
System.out.println("排序后:"
+ Arrays.toString(_ids).replaceAll("[|]", ""));
}
}