?
Solved

order list returned by other api

Posted on 2006-06-09
6
Medium Priority
?
223 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1600 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Assisted Solution

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

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

Assisted Solution

by:ksivananth
ksivananth earned 1600 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 400 total points
ID: 16880195
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 10 hours left to enroll

762 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