java csv排序,JAVA按列对CSV文件进行排序,然后按日期对列进行正确排序

I am trying to figure out why my date sorting isn't working.

I want to sort a CSV file by some select columns(Strings) then by the date column.

However it is not sorting the date correctly - its appears its just ordering it by the first part of the date (25/12/2018) and not the exact date.

public class MultiColumnCsvSort

{

private static final String COLUMN_SEPARATOR = ",";

public static void main(String[] args) throws Exception

{

InputStream inputStream = new FileInputStream("order_lines_file.csv");

List> lines = readCsv(inputStream);

// Create a comparator that sorts primarily by column 0,

// and if these values are equal, by column 2

Comparator> comparator = createComparator(2,1,5);

Collections.sort(lines, comparator);

OutputStream outputStream = new FileOutputStream("output.csv");

String header = "order id, sku, store, location, quantity, date";

writeCsv(header, lines, outputStream);

}

private static List> readCsv(

InputStream inputStream) throws IOException

{

BufferedReader reader = new BufferedReader(

new InputStreamReader(inputStream));

List> lines = new ArrayList>();

// Skip header

String line = reader.readLine();

while (true)

{

line = reader.readLine();

if (line == null)

{

break;

}

List list = Arrays.asList(line.split(COLUMN_SEPARATOR));

lines.add(list);

}

return lines;

}

private static void writeCsv(

String header, List> lines, OutputStream outputStream)

throws IOException

{

Writer writer = new OutputStreamWriter(outputStream);

writer.write(header+"\n");

for (List list : lines)

{

for (int i = 0; i < list.size(); i++)

{

writer.write(list.get(i));

if (i < list.size() - 1)

{

writer.write(COLUMN_SEPARATOR);

}

}

writer.write("\n");

}

writer.close();

}

private static > Comparator>

createComparator(int... indices)

{

return createComparator(MultiColumnCsvSort.naturalOrder(), indices);

}

private static > Comparator

naturalOrder()

{

return new Comparator()

{

@Override

public int compare(T t0, T t1)

{

return t0.compareTo(t1);

}

};

}

private static Comparator> createComparator(

final Comparator super T> delegate, final int... indices)

{

return new Comparator>()

{

@Override

public int compare(List list0, List list1)

{

for (int i = 0; i < indices.length; i++)

{

T element0 = list0.get(indices[i]);

T element1 = list1.get(indices[i]);

int n = delegate.compare(element0, element1);

if (n != 0)

{

return n;

}

}

return 0;

}

};

}

}

Which I took from this post:

However the output looks like the following when I know there are rows with dates between those values:

current sorted file

解决方案

YYYY-MM-DD (ISO 8601 format)

ordering it by the first part of the date (25/12/2018) and not the exact date.

So morph the string to standard ISO 8601 format (YYYY-MM-DD). Values sorted alphabetically will also be chronological.

Parse input strings

Define a formatting pattern to match your inputs.

String input = "25/12/2018" ;

DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" ) ;

LocalDate ld = LocalDate.parse( input , f ) ;

Of course you should cache the DateTimeFormatter object in your real code. Be aware that the java.time classes are thread-safe, and use the immutable objects pattern.

Generate output strings

The java.time classes use the standard ISO 8601 formats by default when generating/parsing strings. So no need to specify a formatting pattern.

String output = ld.toString() ; // Outputs standard format: YYYY-MM-DD

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值