Results 1 to 5 of 5
  1. #1
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default sorting by Comparator

    package firstList;

    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.Comparator;

    class Ab
    {
    String s;
    public Ab(String s)
    {
    this.s = s;
    }

    public String toString()
    {
    return s;
    }
    }
    class SortBasedOnS implements Comparator
    {
    public int compare(Object o1, Object o2)
    {
    Ab ab1 = (Ab)o1;
    Ab ab2 = (Ab)o2;
    return ab1.s.compareTo(ab2.s);
    }
    }
    public class Mgr55
    {
    public static void main(String[] args)
    {
    ArrayList list = new ArrayList();
    list.add("abc");
    list.add(new Ab("abc"));
    System.out.println(list);
    Collections.sort(list,new SortBasedOnS());
    System.out.println(list);
    }
    }
    Here i am trying to sort the String abc and abc which is got through toString()

    but i got java.lang.ClassCastException
    even though i am comparing two strings,y did i got java.lang.ClassCastException
    what is the reason.?

    Thanks

    Daya

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default

    Your Comparator is based on comparing two instances of class Ab.
    You are sticking one Ab and one String in your List, so the Comparator fails when trying to cast the String entry to an Ab.

  3. #3
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Your Comparator is based on comparing two instances of class Ab.
    You are sticking one Ab and one String in your List, so the Comparator fails when trying to cast the String entry to an Ab.
    thanks sir
    how do u come to know that when casting String to Ab has lead to java.lang.ClassCastException

    but there can be ClassCastException when casting Ab to String also right.?

    is it correct what i said.?

    please do reply

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default

    This code:
    Java Code:
    Ab ab1 = (Ab)o1;
    is casting o1 to Ab.
    If o1 is a String then this will fail with a ClassCastException.

    You shouldn't (normally) be storing two unrelated classes in the same List.

  5. #5
    Dayanand is offline Member
    Join Date
    Feb 2011
    Posts
    61
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    This code:
    Java Code:
    Ab ab1 = (Ab)o1;
    is casting o1 to Ab.
    If o1 is a String then this will fail with a ClassCastException.

    You shouldn't (normally) be storing two unrelated classes in the same List.
    thanks a lot sir,!!!!!!!!!!!!!!!!
    superb explaination
    thanks from my bottom of my heart.!!!!!!!!!!!!!!!!!!!!!


    Daya

Similar Threads

  1. Java - Comparator, sorting arrayLists
    By Kerrai in forum New To Java
    Replies: 15
    Last Post: 10-24-2010, 10:41 PM
  2. how to use comparator
    By KidneyinaCooler in forum Advanced Java
    Replies: 2
    Last Post: 07-18-2010, 11:25 AM
  3. Use different comparator for SortedSet
    By linus_k in forum New To Java
    Replies: 0
    Last Post: 11-21-2008, 03:46 PM
  4. How to search with a Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:39 PM
  5. How to write your own Comparator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •