I want to create three methods which I can use to sort an ArrayList, which reads a list of countries from a text file by the name of the country (alphabetically), the amount of habitants and the size of the country. I know how to sort them by the names but I am lost on how to sort by habitants and sizes.
This is how the text file is structured; name of country, habitants, size and capital.
Belgien 10584534 30528 Bryssel
Bosnien 4590310 51129 Sarajevo
Cypern 854000 9250 Nicosia
Serbien 7276195 77474 Belgrad
I've made a method which reads the file and sorts it with Collections.sort(), but as stated earlier I dont know how to even begin on the other two methods.
My code so far:
public class Land {
static boolean valid = true;
public static boolean sortNames() {
File file = new File("europa.txt");
ArrayList<String> list = new ArrayList<String>();
try{
Scanner scan = new Scanner(file);
while(scan.hasNextLine()){
list.add(scan.nextLine());
}
scan.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();}
ListIterator iterator = list.listIterator();
Collections.sort(list);
System.out.println("Country: Inhabitants: Size: Capital: \n");
for (String element : list) {
System.out.println(element);
}
return valid;
}
public static void main(String[] args) {
System.out.print("\n" + sortNames());
}
}
The code as it is now prints:
Country: Inhabitants: Size: Capital:
Albanien 3581655 28748 Tirana
Belgien 10584534 30528 Bryssel
Bosnien 4590310 51129 Sarajevo