This Java program is designed to manage a list of students, each represented by a demo
object with a roll number (num
) and a name (name
). The main functionality of the program is to sort the students based on different criteria.
Here's a summary of what the code does:
- It prompts the user to enter the number of students.
- For each student, it asks the user to input the student's name and creates a new
demo
object with the roll number (which is the current indexi
) and the inputted name. This object is then added to the listli
. - The program sorts the list
li
twice:- First, it sorts the students alphabetically by the first character of their names using a custom
Comparator
. - Then, it sorts the students by their roll numbers using another custom
Comparator
.
- First, it sorts the students alphabetically by the first character of their names using a custom
- After each sort, the program prints out the list of students.
This program can be used in educational institutions or any other scenario where there's a need to manage and sort a list of individuals based on different criteria. For example, a teacher could use this program to sort students by name for attendance purposes, and then sort by roll number for grading purposes. However, please note that the toString
method in the demo
class needs to be modified to return a string representation of the demo
object for the sorted list to be printed correctly. Currently, it's printing the details but returning the default toString
from the Object
class.