acutally above code is sorting employee class objects based on EmpID or based on EmpName

but my requirement is if know the comparator (on which basis sorting should occur) @ runtime how can we achieve that :)

package CollectionsOps;

import java.util.Arrays;
import java.util.Comparator;

/**
 *
 * @author King@java
 */
class employee{

private int empID;
private String name;

public void setName(String ename){

    this.name=ename;
}

public String getName()
{
    return this.name;
}

public void setID(int eID){
    this.empID=eID;
}

public int getID(){
   
    return this.empID;
}

}


class nameComparator implements Comparator{

    public int compare(Object emp1, Object emp2) {
        //throw new UnsupportedOperationException("Not supported yet.");
       
        String e1= ((employee)emp1).getName();
        String e2= ((employee)emp2).getName();
       
        //int r=e1.compareTo(e2);      
       
        return e1.compareTo(e2);
    }
   
   
}
class idComparator implements Comparator{

    public int compare(Object emp1, Object emp2) {
        //throw new UnsupportedOperationException("Not supported yet.");
        int emp1id=((employee)emp1).getID();
        int emp2id=((employee)emp2).getID();
       
        if(emp1id>emp2id)
           
            return 1;
        else if(emp1id<emp2id)
            return -1;
        else
            return 0;
    }
   
}
public class comparatorPractise {

public static void main(String args[]){
   
    employee emp[]=new employee[5];
   
    emp[0]=new employee();
    emp[1]=new employee();
    emp[2]=new employee();
    emp[3]=new employee();
    emp[4]=new employee();
   
   
    emp[0].setID(101);
    emp[0].setName("Charan");
    emp[1].setID(102);
    emp[1].setName("Aman");
    emp[2].setID(104);
    emp[2].setName("Kumar");
   
    emp[3].setID(100);
    emp[3].setName("Brajesh");
    emp[4].setID(103);
    emp[4].setName("Ravi");
   
    for(int i=0;i<emp.length;i++){       
        System.out.println("empID     : "+emp[i].getID()+" empName :"+emp[i].getName());           
    }
  
    Arrays.sort(emp,new idComparator());
   
    System.out.println("__________using IDComparator:__________");
   
    for(int i=0;i<emp.length;i++){               
        System.out.println("empID     : "+emp[i].getID()+" empName :"+emp[i].getName());              
    }
   
   
    System.out.println("__________ using NameComparator:__________");
   
     Arrays.sort(emp,new nameComparator());
    for(int i=0;i<emp.length;i++){       
        System.out.println("empID     : "+emp[i].getID()+" empName :"+emp[i].getName());    
       
    }    
}

}

I couldn't understand much from your question, but I think that meant this:

employee emp[]=new employee[5];
.....
if (order by id) {
   Arrays.sort(emp,new idComparator());
} else if (order by name) {
   Arrays.sort(emp,new nameComparator());
}
for(int i=0;i<emp.length;i++){
 System.out.println("empID : "+emp[i].getID()+" empName :"+emp[i].getName());
}
This article has been dead for over six months. Start a new discussion instead.