排序一个基于一列的二维阵列 [英] Sort a two dimensional array based on one column

查看:97
本文介绍了排序一个基于一列的二维阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我有一个数据,我的数组像下面的

  2009.07.25 20:24消息
2009.07.25 20:17条消息g
2009.07.25 20:25消息B
2009.07.25 20:30消息d
2009.07.25 20:01消息˚F
2009.07.25 21:08群发E
2009.07.25 19:54电文R

我想基于第一列进行排序,所以我最后的数据看起来像这样

  2009.07.25 19:54电文R
2009.07.25 20:01消息˚F
2009.07.25 20:17条消息g
2009.07.25 20:24消息
2009.07.25 20:25消息B
2009.07.25 20:30消息d
2009.07.25 21:08群发E

的第一列是格式YYYY.MM.DD HH:MM的日期和第二列是String


解决方案

  

排序基于一列结果,二维阵列
  第一列是格式YYYY.MM.DD HH:MM的一个日期。和第二列是String


既然你说的2-D阵列,我认为格式的日期......是指一个字符串。这里是code进行排序字符串的2-D阵列[] []:

 进口java.util.Arrays中;
进口了java.util.Comparator;公共类ASDF {    公共静态无效的主要(最终字串[] args){
        最终的String [] []数据=新的String [] [] {
                新的String [] {2009.07.25 20:24,消息},
                新的String [] {2009.07.25 20:17,消息G},
                新的String [] {2009.07.25 20:25,消息B},
                新的String [] {2009.07.25 20:30,消息D},
                新的String [] {2009.07.25 20:01,消息F},
                新的String [] {2009.07.25 21:08,群发E},
                新的String [] {2009.07.25 19:54,消息R}};        Arrays.sort(数据,新的比较<的String []>(){
            @覆盖
            公众诠释比较(最终的String []取值范,最终的String [] ENTRY2){
                最后弦乐时间1 =取值范[0];
                最后弦乐时间2 = ENTRY2 [0];
                返回time1.compareTo(时间2);
            }
        });        对于(最终的String [] S:数据){
            的System.out.println(S [0] ++ S [1]);
        }
    }}

输出:

  2009.07.25 19:54电文R
2009.07.25 20:01消息˚F
2009.07.25 20:17条消息g
2009.07.25 20:24消息
2009.07.25 20:25消息B
2009.07.25 20:30消息d
2009.07.25 21:08群发E

In Java, I have a data in my array like the following

2009.07.25 20:24 Message A
2009.07.25 20:17 Message G
2009.07.25 20:25 Message B
2009.07.25 20:30 Message D
2009.07.25 20:01 Message F
2009.07.25 21:08 Message E
2009.07.25 19:54 Message R

I would like to sort it based on the first column, so my final data can look like this

2009.07.25 19:54 Message R
2009.07.25 20:01 Message F
2009.07.25 20:17 Message G
2009.07.25 20:24 Message A
2009.07.25 20:25 Message B
2009.07.25 20:30 Message D
2009.07.25 21:08 Message E

The first column is a date of format "yyyy.MM.dd HH:mm" and the second column is a String.

解决方案

Sort a two dimensional array based on one column
The first column is a date of format "yyyy.MM.dd HH:mm" and the second column is a String.

Since you say 2-D array, I assume "date of format ..." means a String. Here's code for sorting a 2-D array of String[][]:

import java.util.Arrays;
import java.util.Comparator;

public class Asdf {

    public static void main(final String[] args) {
        final String[][] data = new String[][] {
                new String[] { "2009.07.25 20:24", "Message A" },
                new String[] { "2009.07.25 20:17", "Message G" },
                new String[] { "2009.07.25 20:25", "Message B" },
                new String[] { "2009.07.25 20:30", "Message D" },
                new String[] { "2009.07.25 20:01", "Message F" },
                new String[] { "2009.07.25 21:08", "Message E" },
                new String[] { "2009.07.25 19:54", "Message R" } };

        Arrays.sort(data, new Comparator<String[]>() {
            @Override
            public int compare(final String[] entry1, final String[] entry2) {
                final String time1 = entry1[0];
                final String time2 = entry2[0];
                return time1.compareTo(time2);
            }
        });

        for (final String[] s : data) {
            System.out.println(s[0] + " " + s[1]);
        }
    }

}

Output:

2009.07.25 19:54 Message R
2009.07.25 20:01 Message F
2009.07.25 20:17 Message G
2009.07.25 20:24 Message A
2009.07.25 20:25 Message B
2009.07.25 20:30 Message D
2009.07.25 21:08 Message E

这篇关于排序一个基于一列的二维阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆