I have to create a method that sorts an ArrayList of objects alphabetically according to email and then prints the sorted array. The part that I am having trouble with it sorting it. I have researched it and tried using Collections.sort(vehiclearray); but that didn't work for me. I was that I needed something called a comparator but couldn't figure out how it worked. Will I have to use those or can something like bubble sort or insertion sort work for this kind of thing?
This is the code that I have so far:
public static void printallsort(ArrayList<vehicle> vehiclearray){
ArrayList<vehicle> vehiclearraysort = new ArrayList<vehicle>();
vehiclearraysort.addAll(vehiclearray);
//Sort
for(int i = 0; i < vehiclearraysort.size(); i++)
if ( vehiclearray.get(i).getEmail() > vehiclearray.get(i+1).getEmail())
//Printing
for(i = 0; i < vehiclearraysort.size(); i++)
System.out.println( vehiclearraysort.get(i).toString() + "\n");
}
Vehicleinstead ofvehicle. And probablyprintAllSortandvehicleArraySort.