代码下载:http://www.zuidaima.com/share/1550463693032448.htm
对于web开发中,多个id的排序很实用,有需要的牛人可以下载。
package com.zuidaima; import java.util.Arrays; 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("\\[|\\]", "")); } }
运行截图:
转载请注明出处。