I have a database, where I need to make the user type two criterias they wanna sort the database by. If they for example want to sort it by name and height. It should then first sort it by name, and then height, but not change the first order it made using name.
I've tried to use an array, where I split the input. I use a switch case that accepts a string, where each case represents how they wanna sort it. If user for example type "1 2" where the first part is one sort, and the second is the second sort.
I hope it makes some sense :D
public void sortByTwoCriteria(){
System.out.println("""
1. Sort by name.
2. Sort by height.
3. Sort by power(s).
4. Sort by weakness(ess).
5. Sort by origin FROM earth.
6. Sort by origin NOT from earth.
""");
String userInput = sc.nextLine().toLowerCase();
ArrayList<Superhero> sortedByTwoCriteria = new ArrayList<>();
String[] userInputs = new String[2];
String userSplit = userInputs.split(" ");
String command = userInputs[0];
String userChoice = "";
if (userInputs.length > 1) {
System.out.println("You have only entered one parameter to sort by.");;
}
switch (command){
case "1":
//do something where it sorts by name.
break;
case "2":
//do something where it sorts by height
break;
case "3":
//do something where it sorts by power
break;
case "4":
//do somehting where it sorts by weakness
break;
case "5":
//do soemthing where it sorts by origin FROM earth (I use a boolean for it)
break;
case "6":
//do something where it sorts by origin NOT from earth (I use a booleaen for it)
break;
}
}
Pretty lost tbh. I tried to copy some code from a older project, where it did something simmilar and tried to rewrite it to work for this.