Solved

order list returned by other api

Posted on 2006-06-09
6
197 Views
Last Modified: 2010-03-31
my code is similiar to:

ArrayList a = otherAPI.searchStudent( "registered");

the a is a ArrayList containing the students registered, but I want to sort this list. I don't know how to do it, and I don't want to otherAPI to sort it on DB side, since I will sort list by different column in different situations.

thanks
0
Comment
Question by:keepworking
  • 5
6 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 16874953
you have to use a comparator to sort this
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 16874959
and Collections.sort( list, yourComparator ) ; will do the job
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 400 total points
ID: 16874978
for e.g.,

class StudenComparator implements Comparator{

  private int on ;

  public StudenComparator( int compareOn ){
    on = compareOn ;
  }

  public int compare( Object obj1, Object obj2 ){
    Student s1 = ( Student )obj1 ;
    Student s2 = ( Student )obj2 ;

    if( on == 0 ){
      String name1 = s1.getName() ;
      String name2 = s2.getName() ;
      return name1.compareTo( name2 ) ;
    }else if( on == age ){
//and so on
    }
  }
}
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 400 total points
ID: 16874981
>>}else if( on == age ){

}else if( on == 1 ){ //age
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 400 total points
ID: 16875046
Collections.sort( list, new StudenComparator( 0 ) ) ;

will sort students object based on name!
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 16880195
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now