order list returned by other api

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
keepworkingAsked:
Who is Participating?
 
ksivananthConnect With a Mentor Commented:
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
 
ksivananthCommented:
you have to use a comparator to sort this
0
 
ksivananthCommented:
and Collections.sort( list, yourComparator ) ; will do the job
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ksivananthConnect With a Mentor Commented:
>>}else if( on == age ){

}else if( on == 1 ){ //age
0
 
ksivananthConnect With a Mentor Commented:
Collections.sort( list, new StudenComparator( 0 ) ) ;

will sort students object based on name!
0
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.