sorting - How do i sort data in text file with Java? vol#2 -
i'm trying sort data have in text file. each row of file contains fields:
name,surname,age,weight,height
i tried split each line using peter dolberg's java code - doesn't work duplicate keys. can instead?
simple approach create class personalinfo instance variable name,surname,age,weight,height. write getter , setter methods.
then create array of personalinfo objects(by reading file ).
personalinfo employeeinfo[] = new personalinfo[3];
then define comparator on basis compare. example age -
class agecomparator implements comparator{ public int compare(object ob1, object ob2){ int ob1age = ((personalinfo)ob1).getage(); int ob2age = ((personalinfo)ob2).getage(); if(ob1age > ob2age) return 1; else if(ob1age < ob2age) return -1; else return 0; } }
then can use comparator sort data.
arrays.sort(employeeinfo, new agecomparator());
if wish sort data taking factors consideration can add logic comparator class.
Comments
Post a Comment