java - collections for sorting employee details by id & firstname -
i have written code sort name id , firstname.
import java.io.*; import java.util.*; public class testemployeesort { /** * @param args */ public static void main(string[] args) throws ioexception { // todo auto-generated method stub string option=null; system.out.println("enter on order sorting should done \n1.id \n2.firstname \n3.lastname"); list<employee> coll = name_insert.getemployees(); bufferedreader br=new bufferedreader(new inputstreamreader(system.in)); option=br.readline(); int a=integer.parseint(option); switch(a) { case 1: collections.sort(coll); printlist(coll); break; case 2: collections.sort(coll,new empsortbyfirstname());// sort method printlist(coll); break; case 3: collections.sort(coll,new sortbylastname());// sort method printlist(coll); } } private static void printlist(list<employee> list) { system.out.println("empid\tfirstname\tlastname\tdate of joining\tdate of birth"); (int i=0;i<list.size();i++) { employee e=list.get(i); system.out.println(e.getempid() + "\t" + e.getfirstname() + "\t" + e.getlastname() +"\t" + e.getdate_of_joining()+"\t"+e.getdate_of_birth()); } } }
for sorting id , first name have code in sort_this class
public class empsortbyfirstname implements comparator<employee>{ public int compare(employee o1, employee o2) { return o1.getfirstname().compareto(o2.getfirstname()); }}
similarly id. want change program have input uset on basis want sort. if user gives id, have sort id. if user gives firstname sort first name. want use if statement. if user enters 1 has sort id 2 has sort first name
create map of user input tokens (string, integer, etc.) comparator<employee>
, , use appropriate one.
Comments
Post a Comment