二维数组根据其中一列的数字排序!

tanweijia888 2014-07-11 02:44:35
有一个二维string数组,其中有一列是不重复的数字,比如:
s 2 aaa
r 1 bbb
s 0 ccc
想把该数组按行进行重新排序,按照第二列的数字进行,请问应该如何排序?比如要排序成这样:
s 0 ccc
r 1 bbb
s 2 aaa
求解答!
...全文
534 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanweijia888 2014-07-12
  • 打赏
  • 举报
回复
引用 7 楼 guest6379 的回复:
[quote=引用 3 楼 commanager 的回复:] 给楼主提供一个另类的解决方法吧。你定一个对象,结构为每行数据的数据结构,以第二列为key值,放入treemap中。嘿嘿!
和我想的一样,增加了一点代码,处理可能字符串格式不标准,中间空格很多的自动分割处理。 比如: 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);
        }
    }
}
[/quote] 想问一下foo这个函数返回的是一个二维数组吗?还有如果想要得到排完续后的二维数组,就是调用foo这个函数咯?
tanweijia888 2014-07-12
  • 打赏
  • 举报
回复
引用 14 楼 rui888 的回复:
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()));    
        } 
这个可行咯?
tanweijia888 2014-07-12
  • 打赏
  • 举报
回复
引用 17 楼 guest6379 的回复:
[quote=引用 16 楼 tanweijia888 的回复:] 想问一下foo这个函数返回的是一个二维数组吗?还有如果想要得到排完续后的二维数组,就是调用foo这个函数咯?
不是,前面的那个代码,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);
            }
        }
    }
}
[/quote] 想问一下二维数组里面 for (String[] each : s) { String[] tmp = each[0].split("\\s+"); map.put(Integer.parseInt(tmp[1]), each[0]); } 里面的each[0]表示的是什么意思呢?是s里面每一行的第一个元素吗?
tony4geek 2014-07-12
  • 打赏
  • 举报
回复
恩 可以的、
guest6379 2014-07-12
  • 打赏
  • 举报
回复
引用 16 楼 tanweijia888 的回复:
想问一下foo这个函数返回的是一个二维数组吗?还有如果想要得到排完续后的二维数组,就是调用foo这个函数咯?
不是,前面的那个代码,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);
            }
        }
    }
}
guest6379 2014-07-12
  • 打赏
  • 举报
回复
引用 18 楼 tanweijia888 的回复:
想问一下二维数组里面 for (String[] each : s) { String[] tmp = each[0].split("\\s+"); map.put(Integer.parseInt(tmp[1]), each[0]); } 里面的each[0]表示的是什么意思呢?是s里面每一行的第一个元素吗?
String[] each是一个1维数组,其中的元素不是别的,是数组! 比如: {"s 2 aaa"} 这个是一个数组,1维,包含1个元素,元素类型是字符串 "s 2 aaa"!
guest6379 2014-07-12
  • 打赏
  • 举报
回复
引用 18 楼 tanweijia888 的回复:
想问一下二维数组里面 for (String[] each : s) { String[] tmp = each[0].split("\\s+"); map.put(Integer.parseInt(tmp[1]), each[0]); } 里面的each[0]表示的是什么意思呢?是s里面每一行的第一个元素吗?
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"
tony4geek 2014-07-11
  • 打赏
  • 举报
回复
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()));    
        } 
tony4geek 2014-07-11
  • 打赏
  • 举报
回复
引用 12 楼 tanweijia888 的回复:
[quote=引用 11 楼 rui888 的回复:] 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();  
		}
我好像没表达清楚意思。。我想达到的效果是原来数组的每一行数据进行根据第二列的数字进行重新排序,而不只是第二列的排序。。 s 2 aaa s 0 ccc r 1 bbb 到 r 1 bbb s 0 ccc s 2 aaa 就是整行数据都要移动。。[/quote] 我做错了。不对
tanweijia888 2014-07-11
  • 打赏
  • 举报
回复
引用 11 楼 rui888 的回复:
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();  
		}
我好像没表达清楚意思。。我想达到的效果是原来数组的每一行数据进行根据第二列的数字进行重新排序,而不只是第二列的排序。。 s 2 aaa s 0 ccc r 1 bbb 到 r 1 bbb s 0 ccc s 2 aaa 就是整行数据都要移动。。
tony4geek 2014-07-11
  • 打赏
  • 举报
回复
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();  
		}
tanweijia888 2014-07-11
  • 打赏
  • 举报
回复
引用 6 楼 rui888 的回复:
楼上的不错哦。也可以取出
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();  
		}
想问一下这一段的原理是什么? for (int j = 0; j < num[1].length; j++) { num[j][1]=temp[j]; } 还有这样是根据数字的大小来排序的吗?因为这些数字都是string类型的。。
commanager 2014-07-11
  • 打赏
  • 举报
回复
好心人太多了,我只是给了建议,有的网友竟然将代码写出来了。
业余草 2014-07-11
  • 打赏
  • 举报
回复
引用 6 楼 rui888 的回复:
楼上的不错哦。也可以取出
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();  
		}
写的挺好的,专家就是不一样的,就是大小比较,一维数组搞定,一切都能搞定。最重要的还是,冒泡排序,选择排序之类的初级知识要牢固
guest6379 2014-07-11
  • 打赏
  • 举报
回复
引用 3 楼 commanager 的回复:
给楼主提供一个另类的解决方法吧。你定一个对象,结构为每行数据的数据结构,以第二列为key值,放入treemap中。嘿嘿!
和我想的一样,增加了一点代码,处理可能字符串格式不标准,中间空格很多的自动分割处理。 比如: 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);
        }
    }
}
tony4geek 2014-07-11
  • 打赏
  • 举报
回复
楼上的不错哦。也可以取出
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();  
		}
hedley 2014-07-11
  • 打赏
  • 举报
回复
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();
        }
    }
}
suciver 2014-07-11
  • 打赏
  • 举报
回复
利用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));
		}
commanager 2014-07-11
  • 打赏
  • 举报
回复
给楼主提供一个另类的解决方法吧。你定一个对象,结构为每行数据的数据结构,以第二列为key值,放入treemap中。嘿嘿!
tanweijia888 2014-07-11
  • 打赏
  • 举报
回复
我想让整个数组根据第二列数字进行排序啊。。
加载更多回复(1)

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧