Problems with comparable interface

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ayan biswas
    New Member
    • Mar 2011
    • 4

    Problems with comparable interface

    I got this code form "Head First Java".

    Code:
    class Song implements Comparable<Song>{  
    String art;  
    String tit;  
    Song(String a,String t)  
    {  
    tit=t;  
    art=a;  
    }  
    public int compareTo(Song s)  
    {  
    return title.compareTo(s.getTitle());  
    }  
    String getTitile()  
    {}  
    String getAuthor()  
    {}  
    }  
    class Songsort{  
    Arraylist <Song> sl=new Arraylist<Song>();  
    public static void main(String arg[])  
    {new Songsort.go();  
    }  
    void go(){  
    getSong();  
    Collections.sort(sl);}  
    }  
    void getSong(){  
    //..i/o code  
    }  
    }
    Here we are adding the song objects in the arraylist.Now ,while sorting we are calling sort() method where as a parameter we are passing the entire arraylist of song objects.But how does sort() call compareTo() on a particular song object?And how does
    return title.compareTo (s.getTitle());
    work? which song objects title are we comparing with s.getTtitle();? ?
    Last edited by Dheeraj Joshi; Apr 8 '11, 03:39 AM. Reason: Please use code tags to post code block
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    From Java Doc
    The sorting of object arrays is a little more involved, as the compiler doesn't check everything for you. If the object in the array implements Comparable, then you can just sort the array directly, in whole or in part. Otherwise, you must provide a Comparator to do the sorting for you. You can also provide a Comparator implementation if you don't like the default ordering.
    When you do
    Code:
    Collections.sort(Object[])
    Is calls the default compareTo method. But in your case you have override the method in song class. So override method will be called during sorting.

    Please refer comparable interface document.

    Regards
    Dheeraj Joshi

    Comment

    • ayan biswas
      New Member
      • Mar 2011
      • 4

      #3
      @ Dheeraj thanks for the reply..But I still have confusions..Whe n I call sort() using arraylist of song objects ..the control transfers to compareTo()..no w, the comparison ->this.title.com pareTo(s.getTit le())..in this line is it that each song object is picked up one by one from the list and compared?If yes,then how does the first comparison take place?the first song object is compared to which object?
      Sorry for asking stupid questions..

      Comment

      • Dheeraj Joshi
        Recognized Expert Top Contributor
        • Jul 2009
        • 1129

        #4
        Yes each object must be compared with rest of the object.

        Java's internal sort algorithm is modified merge sort.

        Regards
        Dheeraj Joshi
        Last edited by Dheeraj Joshi; Apr 8 '11, 05:17 AM. Reason: Internal algorithm is modified merge sort.

        Comment

        Working...