如何对文本排序后取某一列数据
【问题】
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
脚本如下:
A1:读取文本
A2:按照date排序后取value值,结果如下:
写好的脚本如何在应用程序中调用,可以参考