i have a Collection for lastnames read in from an employee class. Is it possible to Organize the output of the sorted names after the Sort? I need an output that is similar to the line i have commented out, instead of the generic single line output with lastnames seperated by comma “[Name1, Name2, Name3…]” which i get now from the Collection…
public void alpha1()
{
LinkedList<String> alphalist = new LinkedList<String>();
ListIterator<Employee> iterate = list.listIterator(0);
// System.out.println("First Name" + "\t" + "Last Name" + "\t" + "Salary");
while (iterate.hasNext())
{
emp = iterate.next();
alphalist.add(emp.getlastname());
}
Collections.sort(alphalist, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return Collator.getInstance().compare(o1, o2);
}
});
System.out.println(alphalist);
}
I think better to create
ComparatoronEmployeeobject and sort the original collection itself.Once your collection is sorted, iterate and print your collection using
Employeeobject itself.e.g.