I need to sort the students in array in ascending order of first name.
I tried to do arraylist but it didn't work. Can someone please help me with this?
Student.java
public class Student {
private String firstName;
private String lastName;
private String studentNumber;
Student(String fName,String lName,String sNumber)
{
firstName=fName;
lastName=lName;
studentNumber=sNumber;
}
public Student()
{
}
public void setFirstName(String fName)
{
firstName=fName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String lName)
{
lastName=lName
}
public String getLastName()
{
return lastName;
}
public void setStudentNumber(String sNumber)
{
studentNumber=sNumber;
} public String getStudentNumber()
{
return studentNumber;
}
public String toString() {
return String.format("%-8s %-9s %-8s\n",firstName,lastName,studentNumber );
}
}
StudentTest.java
public class StudentTest {
public static void main( String[] args)
{
Student[] students = new Student[2];
Scanner inputs = new Scanner(System.in);
for(int i = 0 ; i < students.length; i++)
{
System.out.println("enter Student Number");
String sNumber = inputs.nextLine();
System.out.println("enter Student First Name");
String fName = inputs.nextLine();
System.out.println("enter Student Last Name");
String lName = inputs.nextLine();
students[i] = new Student(sNumber,fName, lName);
}
for(int i = 0 ; i < students.length; i++)
{
System.out.print(students[i]);
}
}
}