Mergesort using Comparable - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Mergesort using Comparable

JT87JT87 Posts: 6Member
Was wondering if anyone would be able to help me. I made a comparable class using mergesort and am able to sort things into ints using my compareTo method. However, I can't do it using Strings and would really like to. Posting my code of class CD below and wondering if anyone could tell me how to edit my compareTo code to compare Strings Thanks xx

public class CD implements Comparable
{

private int CDnum;
private double price;
private String name;

public CD(String artist, int number, double cost)
{
name = artist;
CDnum = number;
price = cost;
}

public int compareTo(Object disc)
{
if(price < ((CD) disc).getPrice()) return -1;
else if (price ==((CD) disc).getPrice()) return 0;
else return 1;
}


public String getName()
{
return name;
}

public double getPrice()
{
return price;
}

public String toString()
{
return (price + " " + name + " " + CDnum);
}


}

My mergesort in another class works fine with this. I just want to sort my CD's in alphabetical order. Its a thing for my coursework. If anyone could help would appreciate it thanks xx

Comments

  • zibadianzibadian Posts: 6,349Member
    : Was wondering if anyone would be able to help me. I made a
    : comparable class using mergesort and am able to sort things into
    : ints using my compareTo method. However, I can't do it using Strings
    : and would really like to. Posting my code of class CD below and
    : wondering if anyone could tell me how to edit my compareTo code to
    : compare Strings Thanks xx
    :
    : public class CD implements Comparable
    : {
    :
    : private int CDnum;
    : private double price;
    : private String name;
    :
    : public CD(String artist, int number, double cost)
    : {
    : name = artist;
    : CDnum = number;
    : price = cost;
    : }
    :
    : public int compareTo(Object disc)
    : {
    : if(price < ((CD) disc).getPrice()) return -1;
    : else if (price ==((CD) disc).getPrice()) return 0;
    : else return 1;
    : }
    :
    :
    : public String getName()
    : {
    : return name;
    : }
    :
    : public double getPrice()
    : {
    : return price;
    : }
    :
    : public String toString()
    : {
    : return (price + " " + name + " " + CDnum);
    : }
    :
    :
    : }
    :
    : My mergesort in another class works fine with this. I just want to
    : sort my CD's in alphabetical order. Its a thing for my coursework.
    : If anyone could help would appreciate it thanks xx
    :
    The String class has its own compareTo() method. You can use that to compare 2 strings. For more info see: http://java.sun.com/javase/6/docs/api/java/lang/String.html#compareTo(java.lang.String)
  • JT87JT87 Posts: 6Member
    : : Was wondering if anyone would be able to help me. I made a
    : : comparable class using mergesort and am able to sort things into
    : : ints using my compareTo method. However, I can't do it using Strings
    : : and would really like to. Posting my code of class CD below and
    : : wondering if anyone could tell me how to edit my compareTo code to
    : : compare Strings Thanks xx
    : :
    : : public class CD implements Comparable
    : : {
    : :
    : : private int CDnum;
    : : private double price;
    : : private String name;
    : :
    : : public CD(String artist, int number, double cost)
    : : {
    : : name = artist;
    : : CDnum = number;
    : : price = cost;
    : : }
    : :
    : : public int compareTo(Object disc)
    : : {
    : : if(price < ((CD) disc).getPrice()) return -1;
    : : else if (price ==((CD) disc).getPrice()) return 0;
    : : else return 1;
    : : }
    : :
    : :
    : : public String getName()
    : : {
    : : return name;
    : : }
    : :
    : : public double getPrice()
    : : {
    : : return price;
    : : }
    : :
    : : public String toString()
    : : {
    : : return (price + " " + name + " " + CDnum);
    : : }
    : :
    : :
    : : }
    : :
    : : My mergesort in another class works fine with this. I just want to
    : : sort my CD's in alphabetical order. Its a thing for my coursework.
    : : If anyone could help would appreciate it thanks xx
    : :
    : The String class has its own compareTo() method. You can use that to
    : compare 2 strings. For more info see:
    : http://java.sun.com/javase/6/docs/api/java/lang/String.html#compareTo
    : (java.lang.String)

    Yeah tried that if(name.compareTo((CD) disc).getName()) and it doesn't work either sayn u cant use compareTo in it or something... really confusing me!
  • zibadianzibadian Posts: 6,349Member
    : : : Was wondering if anyone would be able to help me. I made a
    : : : comparable class using mergesort and am able to sort things into
    : : : ints using my compareTo method. However, I can't do it using Strings
    : : : and would really like to. Posting my code of class CD below and
    : : : wondering if anyone could tell me how to edit my compareTo code to
    : : : compare Strings Thanks xx
    : : :
    : : : public class CD implements Comparable
    : : : {
    : : :
    : : : private int CDnum;
    : : : private double price;
    : : : private String name;
    : : :
    : : : public CD(String artist, int number, double cost)
    : : : {
    : : : name = artist;
    : : : CDnum = number;
    : : : price = cost;
    : : : }
    : : :
    : : : public int compareTo(Object disc)
    : : : {
    : : : if(price < ((CD) disc).getPrice()) return -1;
    : : : else if (price ==((CD) disc).getPrice()) return 0;
    : : : else return 1;
    : : : }
    : : :
    : : :
    : : : public String getName()
    : : : {
    : : : return name;
    : : : }
    : : :
    : : : public double getPrice()
    : : : {
    : : : return price;
    : : : }
    : : :
    : : : public String toString()
    : : : {
    : : : return (price + " " + name + " " + CDnum);
    : : : }
    : : :
    : : :
    : : : }
    : : :
    : : : My mergesort in another class works fine with this. I just want to
    : : : sort my CD's in alphabetical order. Its a thing for my coursework.
    : : : If anyone could help would appreciate it thanks xx
    : : :
    : : The String class has its own compareTo() method. You can use that to
    : : compare 2 strings. For more info see:
    : : http://java.sun.com/javase/6/docs/api/java/lang/String.html#compareTo
    : : (java.lang.String)
    :
    : Yeah tried that if(name.compareTo((CD) disc).getName()) and it
    : doesn't work either sayn u cant use compareTo in it or something...
    : really confusing me!
    :
    Here's how to compare the names of 2 CDs:
    [code]
    name.compareTo( [red](([/red]CD[red])[/red] disk[red])[/red].getName() );
    [/code]
    Notice the brackets. The red ones are necessary for the type-casting. The full (name only) CD.compareTo() will be this:
    [code]
    public int compareTo(Object disk) {
    return name.compareTo( ((CD) disk).getName() );
    }
    [/code]
  • JT87JT87 Posts: 6Member
    : : : : Was wondering if anyone would be able to help me. I made a
    : : : : comparable class using mergesort and am able to sort things into
    : : : : ints using my compareTo method. However, I can't do it using Strings
    : : : : and would really like to. Posting my code of class CD below and
    : : : : wondering if anyone could tell me how to edit my compareTo code to
    : : : : compare Strings Thanks xx
    : : : :
    : : : : public class CD implements Comparable
    : : : : {
    : : : :
    : : : : private int CDnum;
    : : : : private double price;
    : : : : private String name;
    : : : :
    : : : : public CD(String artist, int number, double cost)
    : : : : {
    : : : : name = artist;
    : : : : CDnum = number;
    : : : : price = cost;
    : : : : }
    : : : :
    : : : : public int compareTo(Object disc)
    : : : : {
    : : : : if(price < ((CD) disc).getPrice()) return -1;
    : : : : else if (price ==((CD) disc).getPrice()) return 0;
    : : : : else return 1;
    : : : : }
    : : : :
    : : : :
    : : : : public String getName()
    : : : : {
    : : : : return name;
    : : : : }
    : : : :
    : : : : public double getPrice()
    : : : : {
    : : : : return price;
    : : : : }
    : : : :
    : : : : public String toString()
    : : : : {
    : : : : return (price + " " + name + " " + CDnum);
    : : : : }
    : : : :
    : : : :
    : : : : }
    : : : :
    : : : : My mergesort in another class works fine with this. I just want to
    : : : : sort my CD's in alphabetical order. Its a thing for my coursework.
    : : : : If anyone could help would appreciate it thanks xx
    : : : :
    : : : The String class has its own compareTo() method. You can use that to
    : : : compare 2 strings. For more info see:
    : : : http://java.sun.com/javase/6/docs/api/java/lang/String.html#compareTo
    : : : (java.lang.String)
    : :
    : : Yeah tried that if(name.compareTo((CD) disc).getName()) and it
    : : doesn't work either sayn u cant use compareTo in it or something...
    : : really confusing me!
    : :
    : Here's how to compare the names of 2 CDs:
    : [code]:
    : name.compareTo( [red](([/red]CD[red])[/red] disk[red])[/red].getName() );
    : [/code]:
    : Notice the brackets. The red ones are necessary for the
    : type-casting. The full (name only) CD.compareTo() will be this:
    : [code]:
    : public int compareTo(Object disk) {
    : return name.compareTo( ((CD) disk).getName() );
    : }
    : [/code]:


    Tried doing that also but when using my mergesort it won't sort the CD's in alphabetical order. I have it sorting in price anyways think that should be ok for my coursework and I'll just keep up trying otherwise. Thanks for your help xx
  • zibadianzibadian Posts: 6,349Member
    : : : : : Was wondering if anyone would be able to help me. I made a
    : : : : : comparable class using mergesort and am able to sort things into
    : : : : : ints using my compareTo method. However, I can't do it using Strings
    : : : : : and would really like to. Posting my code of class CD below and
    : : : : : wondering if anyone could tell me how to edit my compareTo code to
    : : : : : compare Strings Thanks xx
    : : : : :
    : : : : : public class CD implements Comparable
    : : : : : {
    : : : : :
    : : : : : private int CDnum;
    : : : : : private double price;
    : : : : : private String name;
    : : : : :
    : : : : : public CD(String artist, int number, double cost)
    : : : : : {
    : : : : : name = artist;
    : : : : : CDnum = number;
    : : : : : price = cost;
    : : : : : }
    : : : : :
    : : : : : public int compareTo(Object disc)
    : : : : : {
    : : : : : if(price < ((CD) disc).getPrice()) return -1;
    : : : : : else if (price ==((CD) disc).getPrice()) return 0;
    : : : : : else return 1;
    : : : : : }
    : : : : :
    : : : : :
    : : : : : public String getName()
    : : : : : {
    : : : : : return name;
    : : : : : }
    : : : : :
    : : : : : public double getPrice()
    : : : : : {
    : : : : : return price;
    : : : : : }
    : : : : :
    : : : : : public String toString()
    : : : : : {
    : : : : : return (price + " " + name + " " + CDnum);
    : : : : : }
    : : : : :
    : : : : :
    : : : : : }
    : : : : :
    : : : : : My mergesort in another class works fine with this. I just want to
    : : : : : sort my CD's in alphabetical order. Its a thing for my coursework.
    : : : : : If anyone could help would appreciate it thanks xx
    : : : : :
    : : : : The String class has its own compareTo() method. You can use that to
    : : : : compare 2 strings. For more info see:
    : : : : http://java.sun.com/javase/6/docs/api/java/lang/String.html#compareTo
    : : : : (java.lang.String)
    : : :
    : : : Yeah tried that if(name.compareTo((CD) disc).getName()) and it
    : : : doesn't work either sayn u cant use compareTo in it or something...
    : : : really confusing me!
    : : :
    : : Here's how to compare the names of 2 CDs:
    : : [code]: :
    : : name.compareTo( [red](([/red]CD[red])[/red] disk[red])[/red].getName() );
    : : [/code]: :
    : : Notice the brackets. The red ones are necessary for the
    : : type-casting. The full (name only) CD.compareTo() will be this:
    : : [code]: :
    : : public int compareTo(Object disk) {
    : : return name.compareTo( ((CD) disk).getName() );
    : : }
    : : [/code]: :
    :
    :
    : Tried doing that also but when using my mergesort it won't sort the
    : CD's in alphabetical order. I have it sorting in price anyways think
    : that should be ok for my coursework and I'll just keep up trying
    : otherwise. Thanks for your help xx
    :
    You could also try the compareToCaseInsensitive() method. To debug this, make a list of 2 unsorted CDs and place the result of String.compareTo() into a variable to see what the comparison result of the names is.
Sign In or Register to comment.