解决方案 »

  1.   

    给楼主提供一个另类的解决方法吧。你定一个对象,结构为每行数据的数据结构,以第二列为key值,放入treemap中。嘿嘿!
      

  2.   

    利用java.util.Arrays.sort(T[] a,Comparator<T> comparator);String[][] arr={{"s", "2", "aaa"},{"r", "1" ,"bbb"},{"s" ,"0", "ccc"}};
    Arrays.sort(arr, new Comparator<String[]>(){
    @Override
    public int compare(String[] o1, String[] o2) {
    return o1[1].compareTo(o2[1]);
    }
    });
    for(String[] s:arr){
    System.out.println(Arrays.toString(s));
    }
      

  3.   

    import java.util.Arrays;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    public class StringTest {
        static String[][] sample = {{"s", "2", "aaa"}, {"r", "1", "bbb"}, {"s", "0", "ccc"}};
        public static void main(String[] args) {
            print(sample);
            
            sort(sample, 1);
            
            print(sample);
        }
        
        /**
         * <pre>
         * 排序
         * 
         * date: 2014年7月11日
         * </pre>
         * @author hedley
         * @param s 目标排序二维String数组
         * @param columnIndex 以第几列为排序对象
         */
        static void sort(String[][] s, final int columnIndex) {
            List<String[]> helperList = Arrays.asList(s);
            
            Collections.sort(helperList, new Comparator<String[]>() {
                @Override
                public int compare(String[] o1, String[] o2) {
                    return o1[columnIndex].compareTo(o2[columnIndex]);
                }
            });
            
            s = (String[][])helperList.toArray();
        }
        
        static void print(String[][] s) {
            for (String[] s1 : s) {
                for (String s2 : s1) {
                    System.out.print(s2 + "  ");
                }
                System.out.println();
            }
        }
    }
      

  4.   

    楼上的不错哦。也可以取出String[][] num={{"s", "2", "aaa"},{"r", "1" ,"bbb"},{"s" ,"0", "ccc"}};
    String[] temp = new String[3];
    for (int i = 0; i < num.length; i++) {
    for (int j = 0; j < num[i].length; j++) {
    if (j == 1) {
    temp[i] = num[i][j];
    }
    }
    }
    Arrays.sort(temp);
    for (int j = 0; j < num[1].length; j++) {
    num[j][1]=temp[j];
    }
    for (int i = 0; i < num.length; i++) {
    for (int j = 0; j < num[i].length; j++) {
     System.out.print(num[i][j]+" ");
    }
      System.out.println();  
    }
      

  5.   


    和我想的一样,增加了一点代码,处理可能字符串格式不标准,中间空格很多的自动分割处理。
    比如:
            String[][] s = {
                    {"s      2 aaa"},
                    {"raa 13 bbb"},
                    {"s 0 ccc"},
            };增加正序,反序排列的逻辑。import java.util.Comparator;
    import java.util.TreeMap;public class Demo {
        public String[] foo(String[][] s){
            TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {            @Override
                public int compare(Integer o1, Integer o2) {
                    int r = o1.compareTo(o2);
                    return r; // 反序就 reuturn -r;
                }
            });
            
            for (String[] each : s) {
                String[] tmp = each[0].split("\\s+");
                map.put(Integer.parseInt(tmp[1]), each[0]);
            }
            return map.values().toArray(new String[]{});
        }
        
        public static void main(String[] args) {
            String[][] s = {
                    {"s      2 aaa"},
                    {"raa 13 bbb"},
                    {"s 0 ccc"},
            };
            String[] out = new Demo().foo(s);
            for (String each : out) {
                System.out.println(each);
            }
        }
    }
      

  6.   

    想问一下这一段的原理是什么?
    for (int j = 0; j < num[1].length; j++) {
    num[j][1]=temp[j];
    }
    还有这样是根据数字的大小来排序的吗?因为这些数字都是string类型的。。
      

  7.   

    Arrays.sort  已经排序好了
    那个for是重新赋值第2 列。
    也可以
    String[][] num={{"s", "2", "aaa"},{"r", "1" ,"bbb"},{"s" ,"0", "ccc"},};
    String[] temp = new String[3];
    for (int i = 0; i < num.length; i++) {
    temp[i]=Arrays.toString(num[i]).replaceFirst(".*?,\\s?(\\d+)\\s?,.*","$1");
    }
    Arrays.sort(temp);
    for (int i = 0; i < num.length; i++) {
    for (int j = 0; j < num[i].length; j++) {
    if(j==1){
    System.out.print(temp[i]+" ");;
    }else{
    System.out.print(num[i][j]+" ");
    }
    }
      System.out.println();  
    }
      

  8.   

    我好像没表达清楚意思。。我想达到的效果是原来数组的每一行数据进行根据第二列的数字进行重新排序,而不只是第二列的排序。。
    s 2 aaa             s 0 ccc
    r 1 bbb    到      r 1 bbb 
    s 0 ccc              s 2 aaa
    就是整行数据都要移动。。
      

  9.   

    我好像没表达清楚意思。。我想达到的效果是原来数组的每一行数据进行根据第二列的数字进行重新排序,而不只是第二列的排序。。
    s 2 aaa             s 0 ccc
    r 1 bbb    到      r 1 bbb 
    s 0 ccc              s 2 aaa
    就是整行数据都要移动。。
    我做错了。不对
      

  10.   

    String[][] num={{"s", "2", "aaa"},{"r", "1" ,"bbb"},{"s" ,"0", "ccc"},};
            Map<String, String[]>  m = new TreeMap<String, String[]>();
            for (int i = 0; i < num.length; i++) {
             m.put(Arrays.toString(num[i]).replaceFirst(".*?,\\s?(\\d+)\\s?,.*","$1"), num[i]);
            }
            for(Map.Entry<String, String[]> entry : m.entrySet())    
            {    
                System.out.println(entry.getKey()+": "+Arrays.toString(entry.getValue()));    
            } 
      

  11.   


    和我想的一样,增加了一点代码,处理可能字符串格式不标准,中间空格很多的自动分割处理。
    比如:
            String[][] s = {
                    {"s      2 aaa"},
                    {"raa 13 bbb"},
                    {"s 0 ccc"},
            };增加正序,反序排列的逻辑。import java.util.Comparator;
    import java.util.TreeMap;public class Demo {
        public String[] foo(String[][] s){
            TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {            @Override
                public int compare(Integer o1, Integer o2) {
                    int r = o1.compareTo(o2);
                    return r; // 反序就 reuturn -r;
                }
            });
            
            for (String[] each : s) {
                String[] tmp = each[0].split("\\s+");
                map.put(Integer.parseInt(tmp[1]), each[0]);
            }
            return map.values().toArray(new String[]{});
        }
        
        public static void main(String[] args) {
            String[][] s = {
                    {"s      2 aaa"},
                    {"raa 13 bbb"},
                    {"s 0 ccc"},
            };
            String[] out = new Demo().foo(s);
            for (String each : out) {
                System.out.println(each);
            }
        }
    }

    想问一下foo这个函数返回的是一个二维数组吗?还有如果想要得到排完续后的二维数组,就是调用foo这个函数咯?
      

  12.   


    不是,前面的那个代码,foo是返回1维数组
    要返回2维数组,新的代码中foo2,返回2维数组import java.util.Comparator;
    import java.util.TreeMap;public class Demo {
        public String[] foo(String[][] s){
            TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {            @Override
                public int compare(Integer o1, Integer o2) {
                    int r = o1.compareTo(o2);
                    return r; // 反序就 reuturn -r;
                }
            });
            
            for (String[] each : s) {
                String[] tmp = each[0].split("\\s+");
                map.put(Integer.parseInt(tmp[1]), each[0]);
            }
            return map.values().toArray(new String[]{});
        }
        
        
        public String[][] foo2(String[][] s){
            TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {            @Override
                public int compare(Integer o1, Integer o2) {
                    int r = o1.compareTo(o2);
                    return r; // 反序就 reuturn -r;
                }
            });
            
            for (String[] each : s) {
                String[] tmp = each[0].split("\\s+");
                map.put(Integer.parseInt(tmp[1]), each[0]);
            }
            String[] d1 = map.values().toArray(new String[]{});
            String[][] d2 = new String[d1.length][1];
            for(int i=0;i<d2.length;i++){
                for(int j=0;j<d2[i].length;j++){
                    d2[i][j] = d1[i];
                }
            }
            return d2;
        }
        
        public static void main(String[] args) {
            String[][] s = {
                    {"s      2 aaa"},
                    {"raa 13 bbb"},
                    {"s 0 ccc"},
            };
            Demo d = new Demo();
            String[][] a = d.foo2(s); // foo2返回2维数组
            for (String[] strings : a) {
                for (String string : strings) {
                    System.out.println(string);
                }
            }
        }
    }
      

  13.   


    不是,前面的那个代码,foo是返回1维数组
    要返回2维数组,新的代码中foo2,返回2维数组import java.util.Comparator;
    import java.util.TreeMap;public class Demo {
        public String[] foo(String[][] s){
            TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {            @Override
                public int compare(Integer o1, Integer o2) {
                    int r = o1.compareTo(o2);
                    return r; // 反序就 reuturn -r;
                }
            });
            
            for (String[] each : s) {
                String[] tmp = each[0].split("\\s+");
                map.put(Integer.parseInt(tmp[1]), each[0]);
            }
            return map.values().toArray(new String[]{});
        }
        
        
        public String[][] foo2(String[][] s){
            TreeMap<Integer, String> map = new TreeMap<Integer, String>(new Comparator<Integer>() {            @Override
                public int compare(Integer o1, Integer o2) {
                    int r = o1.compareTo(o2);
                    return r; // 反序就 reuturn -r;
                }
            });
            
            for (String[] each : s) {
                String[] tmp = each[0].split("\\s+");
                map.put(Integer.parseInt(tmp[1]), each[0]);
            }
            String[] d1 = map.values().toArray(new String[]{});
            String[][] d2 = new String[d1.length][1];
            for(int i=0;i<d2.length;i++){
                for(int j=0;j<d2[i].length;j++){
                    d2[i][j] = d1[i];
                }
            }
            return d2;
        }
        
        public static void main(String[] args) {
            String[][] s = {
                    {"s      2 aaa"},
                    {"raa 13 bbb"},
                    {"s 0 ccc"},
            };
            Demo d = new Demo();
            String[][] a = d.foo2(s); // foo2返回2维数组
            for (String[] strings : a) {
                for (String string : strings) {
                    System.out.println(string);
                }
            }
        }
    }

    想问一下二维数组里面
    for (String[] each : s) {
                String[] tmp = each[0].split("\\s+");
                map.put(Integer.parseInt(tmp[1]), each[0]);
            }
    里面的each[0]表示的是什么意思呢?是s里面每一行的第一个元素吗?
      

  14.   

            String[][] s = {
                    {"s      2 aaa"},
                    {"raa 13 bbb"},
                    {"s 0 ccc"},
            };String[] each 是一个1维数组,其中第0个元素是数组{"s      2 aaa"},第1个元素是数组 {"raa 13 bbb"},第2个元素是数组{"s 0 ccc"}each[0]就是1维数组each的第0个元素,
    当each是数组{"s      2 aaa"}的时候,each[0]就是"s      2 aaa"
    当each是数组 {"raa 13 bbb"}的时候,each[0]就是"raa 13 bbb"假设each是数组{"hello world", "s      2 aaa"}的时候,each[0]就是"hello world"
      

  15.   


    String[] each是一个1维数组,其中的元素不是别的,是数组!
    比如: {"s      2 aaa"} 这个是一个数组,1维,包含1个元素,元素类型是字符串 "s      2 aaa"!