• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Sorting  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. I'm a pretty big java noob and would really appreciate some assistance. I'm creating a java program that stores data in defined array elements and then sorts them based on their initial string values. I also need to add up the last int value in each array element. I am struggling with how to sort them particularly (I've tried using arrays.sort() method, but recieve a ClastCastException, stating that the class of my array cannot be cast to java.lang.comparable. I don't get any errors when compiling, so I'm kind of at a loss as to how to attack this problem.

Here's my code:
 
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel, in about five minutes or so, a very helpful Bartender named "Winston" is going to reformat your post for you, so the code is easier to read. Check out how he does it as it's a very useful thing to know, and makes your code look like this:


And here's Camera Class:


Now let me mull over your problem for a minute...
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel, I believe your Camera class must implement the Comparable interface. Look at the docs on the Arrays method you are using.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Kavanaugh wrote: I am struggling with how to sort them particularly (I've tried using arrays.sort() method, but recieve a ClastCastException, stating that the class of my array cannot be cast to java.lang.comparable. I don't get any errors when compiling, so I'm kind of at a loss as to how to attack this problem.


Arrays.sort() can't magically sort everything -- think about it, how would you create a sorter that can sort anything? You have to have some idea of an order to be able to sort it.

This is accomplished in two possible ways.... First, you can make your object (in this case, your camera object) Comparable. This provides a way for the sorter to be able to compare if a Camera is less than or greater than another camera. Second, you can also provide a Comparator to the sorter, so that it can can compare two cameras.

In your case, you didn't provide a comparator. And your camera objects are not comparable, so, there is no way for Arrays.sort() to figure out an order to it, and hence, be able to sort it.

Henry
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:Daniel, in about five minutes or so, a very helpful Bartender named "Winston" is going to reformat your post for you, so the code is easier to read.
Check out how he does it as it's a very useful thing to know

Sorry guys. 45 minutes late (I started a thread of my own).

@Daniel: Please look at the UseCodeTags (←click) page. And please read it thoroughly, we don't like really long lines (I already split yours).

Winston
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@daniel

Refer this question. StackOverflow- ClassCastException
 
Daniel Kavanaugh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

Stevens Miller wrote:Daniel, I believe your Camera class must implement the Comparable interface. Look at the docs on the Arrays method you are using.


I've read this on another site as well, but I'm having trouble in actually writing the code needed to do this.
I looked at the docs you provided and created a public static void sort(Object[] a) method in my Camera class, but wasn't sure what to fill it with. I think I'm missing what exactly this really means.

@Henry How might I create a comparator?

@Prasad I'm having difficulty understanding how that question works, as I don't see where valueOfFood comes from

@Winston sorry for being sloppy with my posting, I'll be sure to use code tags from now on.
 
Prasad prap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a close look at this example:
Comparator example


Let me know if you have trouble fitting that in your example.
 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few things to consider:

1. Another protocol we have here: SearchFirst (← click) - Search term "java comparator examples" turns up some good links

2. Don't bother trying to implement that sort(Object[] a) method in Camera class; that's the wrong way to go. Stick with implementing a Comparator for Camera instead.

3. The original code you posted doesn't make a whole lot of sense to me, even if it were to execute without errors. You first set up a list of cameras. Then you expect the user to type in a camera name. You then go through your list and check if the user's input matches one of the cameras you set up. When you find a match, you display the camera information name (Edit: sorry, realized that you're displaying all the camera info via the overridden toString(), not just the name). I don't get the point; there doesn't seem to be anything useful happening in the program. And So, what does sorting the list have to do with any of this other stuff that's happening?

4. Also, you have to write more if-nameOfProduct.equalsIgnoreCase statements every time you add a camera to your initial list. That's not really a good thing either.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Kavanaugh wrote:How might I create a comparator?


Whenever I have the question "How do I do X in Java?" then my Google keywords are java x tutorial. In the case of java comparator tutorial the first link I get is the Oracle tutorial (which you should look out for in whatever list your search engine deems to be the answer) Object Ordering. It's all in there.
 
Daniel Kavanaugh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad prap wrote:Have a close look at this example:
Comparator example


Let me know if you have trouble fitting that in your example.


Thanks for the link, it really helped. My code is almost exactly the same as that, but I'm still getting 3 identifier expected errors in the public class Camera implements Comparable<Camera> line. I've attached the file. I'm probably missing something very obvious, but it eludes me.



(Sorry that it's above the character count)
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are lines 13 and 14 doing for you?
 
Prasad prap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel, Can you post just the snippet(No getter/setter) in the code and the exact error at that line. Things will be lot more easier for everyone.

Anyways,

what is this line just above your constructor doing?

Public class camera implements Comparable


1. Public should be "public".
2. Use case not clear.
3. I believe you used the example literally. I can see Fruit references. Check that out.
 
Daniel Kavanaugh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Prasad prap, I apologize for still being unable to do this, but I am having further errors. Should I be approaching this a different way, or do you think I can make this eventually work? Thanks so much for your help.

Camera.java:3: error: Camera is not abstract and does not override abstract method compareTo(Camera) in comparable
public class Camera implements Comparable<Camera>

Camera.java:53: error: compareTo(Camera) in Camera cannot implement compareTo(T) in comparable
public String compareTo(Camera compareCamera)

return type String is not compatible with int
where T is a type-variable:
T extends Object declared in interface Comparable
Camera.java:64: error: non-static variable productName cannot be referenced from a static context
String productName1 = camera1.getProductName(productName);
Camera.java:65: error: non-static variable productName cannot be referenced from a static context
String productName2.getProductName(productName);


 
Prasad prap
Greenhorn
Posts: 28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Daniel never mind. Thats why this is beginner Java. Do not worry. Shoot your efforts.
Whenever you implement an interface you have to write a definition for methods in the interface.

For e.g:
DO NOT COMPILE THIS CODE. Just observe its concept implementation.



So did you get it why this is wrong?

public static Comparator<Camera> ProductNameComparator
= new Comparator<Camera>()
{
public int compare(Camera camera1, Camera camera2)
{
String productName1 = camera1.getProductName(productName);
String productName2 = camera2.getProductName(productName);

return productName1.compareTo(productName2);

}
};



Let me know if you still need elaboration.
 
Daniel Kavanaugh
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't I define all the needed methods?
 
Prasad prap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please watch the signature correctly.

Its is and not .

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!