如何对文本排序后取某一列数据

【问题】

i have csv as input with 2 columns and want to sort data on basis of date column (dd/mm/yyyy). below code sort the date correctly but i want the related value with that date... output of this code is like

 02/05/2012

 09/11/2012

 10/11/2012

Code:

public static void main(String[] args){
    Date value = null;
    String reader ="";
    String[] input = null ;
    Date date;
    List<Date> dateList = new ArrayList<Date>();
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    File file = new File("data.csv");

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        while((reader = br.readLine())!=null){             
            input = reader.split(",");             
             date = df.parse(input[0]);
             dateList.add(date);
        }

        Collections.sort(dateList, new Comparator<Date>() {
             public int compare(Date o1, Date o2){
                return o1.compareTo(o2);
             }
        });

         for(Date x : dateList){
             System.out.println(df.format(x));              
         }
    } catch (FileNotFoundException fi) {
       fi.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch(ParseException pe){
        pe.printStackTrace();
    }      
}

【回答】

       对记录排序并取某字段,这是最简单的结构化算法,但JAVA缺乏相关的类库,需要自行编写大量代码。这种情况下可以用SPL辅助JAVA来实现。

       假设原始数据是:

date,value

10/11/2012,300

02/05/2012,100

09/11/2012,200

       脚本如下:

A
1=file("d:\\data.csv").import@t(;,",")
2=A1.sort(date).(value)

A1:读取文本

A2:按照date排序后取value值,结果如下:

写好的脚本如何在应用程序中调用,可以参考Java 如何调用 SPL 脚本

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值