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]); 
        } 
 
} 
}

Recommended Answers

All 4 Replies

Collections.sort(yourArrayList);

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort%28java.util.List%29

And mark your other thread solved, since you're apparently finished with it.

I tried doing that but it didn't work maybe because my array has 3 strings(snumber,fname,lname). I did this

ArrayList<Student> students = new ArrayList<Student>(5);{
for...
students.add(new Student(sNumber,fName, lName));
} Collections.sort(students);

As the documentation that I linked you to previously states,

All elements in the list must implement the Comparable interface

So make your class implement Comparable, then write the compareTo method. Here is how to write compareTo.

I can't emphasize enough that it is important to learn how to use the Javadoc documentation. I'm happy to help you as much as you need it, but to become an independent programmer, it is important to be able to read it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.