I have an arraylist that has integers and string i want to sort the values when requested.
Example: if the user selected t from menu it would sort the student IDs or if the user selects f it would sort the students first names etc.
This is what I have so far, stuID method doesn't work when it is called. How can I fixed this and how do I sort the first names when method is called?
public class Student implements Comparable <Student>{ private int idNum; private String fName; private String surname; private int entryYear; public int getIdNum() { return idNum; } public void setIdNum(int idNum) { this.idNum = idNum; } public String getfName() { return fName; } public void setfName(String fName) { this.fName = fName; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } public int getEntryYear() { return entryYear; } public void setEntryYear(int entryYear) { this.entryYear = entryYear; } public Student(int idNum, String fName, String surname, int entryYear) { super(); this.idNum = idNum; this.fName = fName; this.surname = surname; this.entryYear = entryYear; } public Student(int idNum) { super(); this.idNum = idNum; } @Override public String toString() { return "Student [idNum=" + idNum + ", fName=" + fName + ", surname=" + surname + ", entryYear=" + entryYear + "]"; } @Override public int compareTo(Student s) { if(this.getIdNum() > s.getIdNum())// return 1; else return -1; } // public int compare(Student fn) { // if(this.getfName().equals(fn.getfName()))//list all first names stored // return 1; // else // return -1; // } } import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class StudentTest { static ArrayList<Student> stu = new ArrayList<Student>(); public static void main(String[] args) { Scanner sc= new Scanner(System.in); char opt= ' '; char menu= ' '; Student s1= new Student(3, "Alice", "Zen", 1999); Student s2= new Student(1, "John", "son", 1998); Student s3= new Student(4, "Gin", "mora", 2000); Student s4= new Student(1, "ola", "deed", 2001); Student s5= new Student(5, "Marcus", "seph", 2004); Student s6= new Student(6, "Gary", "moore", 1996); stu.add(s1); stu.add(s2); stu.add(s3); stu.add(s4); stu.add(s5); stu.add(s6); //menu System.out.println("Choose \nN- New student" + "\nT- Sort student ID" + "\nF- Sort student first name" + "\nL- Sort student last name"+ "\nE- Sort student entry year" +"\nX- Exit"); menu = sc.next().charAt(0);//main menu input(choice is Main Menu //menu choices while( menu == 'x') { if ((menu == 'N') || (menu == 'n')) { System.out.println("You chose Add!"+ "/nPlease enter new student information"); } else if((menu == 'T')|| (menu == 't')) { System.out.println("sort students ID numbers"); stuID(); } else if((menu == 'F')|| (menu == 'f')) { System.out.println("sort students first names"); //stuFn(); } else if((menu == 'L')|| (menu == 'l')) { System.out.println("sort students last name"); } else if((menu == 'E')|| (menu == 'e')) { System.out.println("sort students entry year"); } else if((menu == 'X')|| (menu == 'x')) { System.out.println("Good Bye!!"); } else System.out.println("Invalid option!!"); } } public static void stuID() { //sort id numbers listed Collections.sort(stu); for(Student st : stu) { //loop elements in array System.out.println(st);// print elements in array } } https://stackoverflow.com/questions/66631502/how-to-sort-different-data-type-vales-of-the-same-arraylist March 15, 2021 at 10:05AM
没有评论:
发表评论