僞代碼:java
產品代碼:git
import java.util.*; public class MySort { public void mySort(String[] toSort, int k) { System.out.println("Before sort:"); for (String str : toSort) { System.out.println(str); } int length = toSort.length; int[] a = new int[length]; if (k == 1) { for (int i = 0; i < length; i++) { a[i] = Integer.parseInt(toSort[i].split(":")[k]); } } Arrays.sort(a); System.out.println("After sort:"); for (int i = 0; i < length; i++) { for (int j = 0; j < length; j++) { if (a[i] == Integer.parseInt(toSort[j].split(":")[k])) { System.out.println(toSort[j]); } } } } }
測試代碼:數組
public class MySortTest { public static void main(String[] args) { String[] toSort = {"aaa:10:1:1", "ccc:30:3:4", "bbb:50:4:5", "ddd:20:5:3", "eee:40:2:20"}; int a = Integer.parseInt(args[0]); MySort mysort = new MySort(); mysort.mySort(toSort, a); } }
碼雲連接測試