日期:2014-05-20  浏览次数:20701 次

java 多个数组排序问题!!过完节的都进来看看!!
[code=Java]

String   sprotAry1   []   =   { "1 ", "01 ", "12345 ", "2 ", " "};
String   sprotAry2   []   =   { "2 ", "02 ", "712345 ", "2 ", " "};
String   sprotAry3   []   =   { "3 ", "02 ", "62345 ", "2 ", " "};
String   sprotAry4   []   =   { "4 ", "03 ", "512345 ", "2 ", " "};

我把它们装在list   中   现在要对上面的数组进行排序!
原则是   根据第二个和第三个元素排序
也就是列如:   sprotAry1   中的   01   和   12345     和下一个   sprotAry2   中的   02   和   712345   做比较。
我期望的结果是   返回的list中的顺序是   sprotAry1   sprotAry3   sprotAry2   sprotAry4  
不知道表达明白没。。。

[/code]

------解决方案--------------------
Java code

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();
        }
    }

}

------解决方案--------------------
Java code

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();
        }
    }
}