java - Sorting Arraylist with multiple data types by date (numeric) -



java - Sorting Arraylist with multiple data types by date (numeric) -

this have. im scanning txt , passing array string (per line). need sort date

bundle stocktest; import java.io.filenotfoundexception; import java.util.arraylist; import java.util.scanner; /** */ public class stocktest { /** * @param args command line arguments */ public static void main(string[] args) { arraylist alist = new arraylist(); java.io.file file = new java.io.file("transactions.txt"); seek { scanner input = new scanner(file); while (input.hasnext()) { string info = input.nextline(); alist.add(data); system.out.println(alist); } } grab (filenotfoundexception e) { system.err.format("file not exist/n"); } } }

each line in transaction.txt looks this

buy,1/2/2002,ibm,30,135.00

i need sort list date. think should do? in advance.

first of why dont seek parse each line "," , set class have states , methods, can utilize comparator sort array list see example.

private list<orderdto> orderdtos;

public class orderdtodatedesccomparator implements comparator<orderdto> { @override public int compare(orderdto orderdto, orderdto other) { if (orderdto.getdatecreated() != null && other.getdatecreated() != null) { homecoming orderdto.getdatecreated().compareto(other.getdatecreated()); } else if (orderdto.getdatecreated() != null && other.getdatecreated() == null) { homecoming 1; } else if (orderdto.getdatecreated() == null && other.getdatecreated() != null) { homecoming -1; } homecoming 0; } }

collections.sort(this.orderdtos, new orderdtodatedesccomparator());

see easy. is.

java sorting arraylist

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -