我已经编写了一个用ID和名字排序名称的代码。按ID和名字排序员工详细信息的集合
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 which order sorting should be 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");
for (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());
}
}
}
用于通过ID和第一名称排序我在Sort_this类
public class EmpSortByFirstName implements Comparator<Employee>{
public int compare(Employee o1, Employee o2) {
return o1.getFirstName().compareTo(o2.getFirstName()); }}
类似地对于ID此代码。 现在我想改变我的程序,就像我必须从你想要排序的基础上得到输入。如果用户给出id,我必须按id排序。如果用户给出名字,然后按名字排序。我想用if语句。如果用户输入1,则必须按ID 2排序,它必须按名排序
您遇到的问题是什么?你似乎已经有了做你想做的事的代码。 – casablanca 2010-12-01 05:36:22
你是什么意思'如果用户给firstname'。用户应该在输入中输入'名字'? – 2010-12-01 05:37:49