How to use sorting technic in arralist?

Showing Answers 1 - 15 of 15 Answers

Djava

  • Aug 11th, 2009
 

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.

Djava

  • Aug 14th, 2009
 

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.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions