GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Java Patterns
Go To First  |  Previous Question  |  Next Question 
 Java Patterns  |  Question 18 of 20    Print  
How to use sorting technic in arralist?

  
Total Answers and Comments: 5 Last Update: August 16, 2009     Asked by: subrahmanyam 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: sampra
 
java.util.Collections.sort(list)

Above answer was rated as good by the following members:
adrianpredam, Sarje
November 28, 2007 18:18:42   #1  
pavankantamaneni Member Since: June 2006   Contribution: 1    

RE: How to use sorting technic in arralist?
java.util.Collections.sort(list)
Sorts the specified list into ascending order according to the natural ordering of its elements.

 
Is this answer useful? Yes | No
March 10, 2008 05:04:14   #2  
sampra Member Since: February 2008   Contribution: 278    

RE: How to use sorting technic in arralist?
java.util.Collections.sort(list)
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 16, 2009 07:50:18   #3  
jeyarajg Member Since: June 2009   Contribution: 1    

RE: How to use sorting technic in arralist?
java.util.Collections.sort( list comparator );

The comparator will alter the way the list is sorted.

 
Is this answer useful? Yes | No
August 11, 2009 13:42:32   #4  
Djava Member Since: August 2009   Contribution: 4    

RE: How to use sorting technic in arralist?
If you are having list of 'natural' things like numbers(1 2 3 etc.) Or String lets say ("ABC" "BCD" etc.). You can get natural sorting using.

java.util.Collections.sort(list); where list can be of type numbers or string etc.

But if you want to sort a list of any "Object" based upon certain parameter then You will have to implement an interface called "Comparable" and will have to override it method "compareTo(ObjectClass object)"

Lets take an example.

Lets say you have a class "BookStore" which is a Arraylist of Book
Arraylist<Book> list new ArrayList<Book>();

Class "Book" is having following data members fields
1. AuthorTitle (String)
2. BookTitle (String)

If you want to sort the books list according to author name you will have to implement
interface "Comparable" in Class "Book". And the overriden method would be something like this.

public int compareTo(Book bookObject) {
return this.AuthorTitle.compareTo(bookObject.AuthorTitle);
}

Remember that "String" Class also implements comparable so we were able to use compareTo methos as defined in "String".

Now If we have to sort a list we can simply use.

java.util.Collections.sort(list);

It will sort my list according to AuthorTitle in ascending order.


Now What if I want to sort my list according to BookTitle. Without changing the original code for Book.

For that We use "Comparator".

write a new Class like "BookTitleSort" and implement "Comparator" interfce also just override this method in it:

public int compare(Book bookObject1 Book bookObject2) {

return bookObject1.getBookTitle().compareTo(bookObject2.getBookTitlte());
}


Once you have written this class you can sort your list using this code:

BookTitleSort bookTitleSortObject new BookTitleSort ();
java.util.Collections.sort(list bookTitleSortObject);


This will sort your list according to BookTitle.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 14, 2009 08:45:07   #5  
Djava Member Since: August 2009   Contribution: 4    

RE: How to use sorting technic in arralist?
If you are having list of 'natural' things like numbers(1 2 3 etc.) Or String lets say ("ABC" "BCD" etc.). You can get natural sorting using.

java.util.Collections.sort(list); where list can be of type numbers or string etc.

But if you want to sort a list of any "Object" based upon certain parameter then You will have to implement an interface called "Comparable" and will have to override it method "compareTo(ObjectClass object)"

Lets take an example.

Lets say you have a class "BookStore" which is a Arraylist of Book
Arraylist<Book> list new ArrayList<Book>();

Class "Book" is having following data members fields
1. AuthorTitle (String)
2. BookTitle (String)

If you want to sort the books list according to author name you will have to implement
interface "Comparable" in Class "Book". And the overriden method would be something like this.

public int compareTo(Book bookObject) {
return this.AuthorTitle.compareTo(bookObject.AuthorTitle);
}

Remember that "String" Class also implements comparable so we were able to use compareTo methos as defined in "String".

Now If we have to sort a list we can simply use.

java.util.Collections.sort(list);

It will sort my list according to AuthorTitle in ascending order.


Now What if I want to sort my list according to BookTitle. Without changing the original code for Book.

For that We use "Comparator".

write a new Class like "BookTitleSort" and implement "Comparator" interfce also just override this method in it:

public int compare(Book bookObject1 Book bookObject2) {

return bookObject1.getBookTitle().compareTo(bookObject2.getBookTitlte());
}


Once you have written this class you can sort your list using this code:

BookTitleSort bookTitleSortObject new BookTitleSort ();
java.util.Collections.sort(list bookTitleSortObject);


This will sort your list according to BookTitle.

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape