1,105,456 Community Members

Sorting Array of Strings in ascending order

Member Avatar
need
Newbie Poster
5 posts since Sep 2009
Reputation Points: 28 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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]); 
        } 
 
} 
}
Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 
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.

Member Avatar
need
Newbie Poster
5 posts since Sep 2009
Reputation Points: 28 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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);

Member Avatar
BestJewSinceJC
Posting Maven
2,760 posts since Sep 2008
Reputation Points: 700 [?]
Q&As Helped to Solve: 366 [?]
Skill Endorsements: 17 [?]
 
0
 

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.

Member Avatar
AbhikGhosh
Light Poster
41 posts since Jan 2010
Reputation Points: -1 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

hi pls check my post at this thread

http://www.daniweb.com/forums/thread253670.html

this is already solved

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article