日期:2014-05-20 浏览次数:20731 次
import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Sort { static final String[][] array = { { "1", "01", "12345", "2 ", " " }, { "2", "01", "12345", "2 ", " " }, { "3", "02", "712345", "2 ", " " }, { "4", "03", "2344", "2 ", " " }, { "5", "01", "12348", "2 ", " " }, { "6", "02", "62345", "2 ", " " }, { "7", "01", "2344", "2 ", " " }, { "8", "03", "512345", "2 ", " " } }; /** * @param args */ public static void main(String[] args) { List<String[]> lists = Arrays.asList(array); Collections.sort(lists, new Comparator<String[]>() { public int compare(String[] o1, String[] o2) { try { int o1Order1 = Integer.parseInt(o1[1]); int o2Order1 = Integer.parseInt(o2[1]); int o1Order2 = Integer.parseInt(o1[2]); int o2Order2 = Integer.parseInt(o2[2]); if (o1Order1 == o2Order1 && o1Order2 < o2Order2) { return -1; } else if (o1Order1 < o2Order1) { return -1; } } catch (Exception ex) { } return 1; } }); print(lists); } private static void print(List<String[]> lists) { for (String[] prints : lists) { for (String print : prints) { System.out.print(print + " "); } System.out.println(); } } }
------解决方案--------------------
package cn.geniusman.main; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Sort { static final String[][] array = { { "1", "01", "12345", "2 ", " " }, { "2", "01", "12345", "2 ", " " }, { "3", "02", "712345", "2 ", " " }, { "4", "03", "2344", "2 ", " " }, { "5", "01", "12348", "2 ", " " }, { "6", "02", "62345", "2 ", " " }, { "7", "01", "2344", "2 ", " " }, { "8", "03", "512345", "2 ", " " } }; /** * @param args */ public static void main(String[] args) { List<String[]> lists = Arrays.asList(array); Collections.sort(lists, new Comparator<String[]>() { public int compare(String[] o1, String[] o2) { try { int o1Order1 = Integer.parseInt(o1[1]); int o2Order1 = Integer.parseInt(o2[1]); int o1Order2 = Integer.parseInt(o1[2]); int o2Order2 = Integer.parseInt(o2[2]); if (o1Order1 == o2Order1 && o1Order2 < o2Order2) { return -1; } else if (o1Order1 < o2Order1) { return -1; } } catch (Exception ex) { } return 1; } }); printAndSetOrder(lists); } private static void printAndSetOrder(List<String[]> lists) { int index = 0; for (String[] prints : lists) { prints[4] = String.valueOf(++index); for (String print : prints) { System.out.print(print + " "); } System.out.println(); } } }