Search code examples
javastringsortingcollectionscompareto

assist me to understand behind the scenes of title.compareTo(a.title()); codeInside


//Sorting userDefined object from ArrayList<>...    

import java.io.*;
import java.util.*;

class Song implements Comparable<Song>{
    String title;
    String movie;
    String rating;

    public int compareTo(Song s){
        //System.out.println(getTitle()+"   "+s.getTitle());
        /*The upper comment is for testing purpose, 
         *because i wanted to see whats the value getTitle() compares..
         *but i couldn't understand.
         */
        return getTitle().compareTo(s.getTitle());
    }

    public Song(String t, String m, String r){
    //R.I.P. Naming Convention.
        title = t;
        movie = m;
        rating = r;
    }

    public String getTitle(){
        return title;
    }

    public String toString(){
        return title;
    }
}

class ArrayListDemo{
    ArrayList<Song> songsList = new ArrayList<Song>();
    public static void main(String[] args){
        new ArrayListDemo();
    }

    public ArrayListDemo(){
        getSongs();
        System.out.println(songsList); 
        Collections.sort(songsList);   
        System.out.println(songsList);
    }

    public void getSongs(){
        try{
            File file = new File("SongsList.txt");
            //check below for SongsList.txt 
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;
            while((line = reader.readLine()) != null){
                addSong(line);
            }
        } catch(IOException e){
            e.printStackTrace();
        }
    }

    public void addSong(String lineToParse){
        String[] token = lineToParse.split("/");
        Song nextSong = new Song(token[0], token[1], token[2]); 
        songsList.add(nextSong);
    }
}

SongsList.txt
Soch Na Sake / Airlift / 9.1
Jeena / Badlapur / 8.7
Tere Sang Yaara / Rustom / 8.8
Aayat Ki Tarah / BaajiravMastaani / 7.9
Ikk Kudi / UdtaPunjab / 7.5
Tay Hai / Rustom / 7.8

Output:-
Before Sorting...
[Soch Na Sake , Jeena , Tere Sang Yaara , Aayat Ki Tarah , Ikk Kudi , Tay Hai ]

After Sorting...
[Aayat Ki Tarah , Ikk Kudi , Jeena , Soch Na Sake , Tay Hai , Tere Sang Yaara ]

Caution:- Beginner Level English Ahead!!!

So this is working of my program... Reference :-HeadFirstJava 2nd , JukeBox3 Page no.- 550

So, coming to the problem..

i understood most of them...but this is where my mind is rolling.. o.O

public int compareTo(Song s){
    return getTitle().compareTo(s.getTitle());
}

getTitle() & s.getTitle()

from where getTitle() gets value and compares it... ok, i know that compareTo() compares String but and i also know the -1,0,1 (<,=,>) rule, the thing which cracking me up is from where getTitle() gets the value. and one more thing... the book says that

Collections.sort(songsList);

when this method gets called, the sort method sends an element from songsList to compareTo() method.. means s.getTitle() = title of the element sent bye sort(). Right ?

but from where the first getTitle() gets the value... the one which is after the return and before the .compareTo().

Kindly assist me here, checked docs, other answers, everything i can in last two days....

in simple words i want to know that from where and which values getTitle() gets to compare.


Solution

  • public int compareTo(Song s) is used to compare 2 Song objects. One of them is the one the method is called on, and the other is passed as an argument.

    For example :

    Song s1 = ...
    Song s2 = ...
    int s1.compareTo(s2);
    

    In this example, getTitle() in the body of public int compareTo(Song s) will return the title of s1, while s.getTitle() wil return the title of s2.

    Collections.sort(songsList); will always use compareTo to compare 2 Song objects - one of them it will call the method on, and the other will be passed as an argument.