Here record is an ArrayList of objects of the type Employee. I am trying to sort the ArrayList based on the employeeName attribute. Unfortunately, it gives unwanted results.
public void sortByName(){
for(int i = 0; i < SalesDataManager.N; i++){
for(int j = i+1; j < SalesDataManager.N; j++){
if(record.get(i).getEmployeeName().compareToIgnoreCase(record.get(j).getEmployeeName()) > 0){
Employee etemp = record.get(i);
record.add(i,record.get(j));
record.add(j,etemp);
}
}
}
displayAllRecords();
}
I have gone through other posts in stackoverflow regarding this topic and found out that most of the post suggest this same way.Am I doing something wrong here?
Thanks in advance!