-3

I need to write a method called search that takes an array of objects in ascending order of a property(in my case Value) and a single object. Uses the compareTo that i have created to answer if the given object is found in the array.

I'm having a hard time thinking of how to do this.

I know theres a method in java.util.Arrays; somewhere surely, i tried Arrays.sort(_traesure); but this lead to error: Treasure cannot be cast to java.lang.Comparable i imagine because it doesn't know what its sorting by.

I need to pass an array of objects in ascending order of their value property.

Ive seen a lot of posts talking about Comparator's but i'm not too sure what they are/do?

Could anyone point me in the right direction.

Erik Vorcov
  • 51
  • 1
  • 8
  • 1
    just to clarify: you want to sort the array first and the search the array to see if this object is in that array??? –  Mar 27 '15 at 23:06
  • Read this: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html – Reut Sharabani Mar 27 '15 at 23:06
  • try this link may be it will help http://stackoverflow.com/questions/18895915/how-to-sort-an-array-of-objects-in-java – Iftikhar Ali Ansari Mar 27 '15 at 23:07
  • What kind of objects the array has? – m0skit0 Mar 27 '15 at 23:11
  • I see this, so its like a compareTo method or an equals method that you make and i guess pass said object(array) and the property in which to sort by? _array.getName(); – Erik Vorcov Mar 27 '15 at 23:11
  • You say that the method will accept a sorted array. Then why do you want to sort the array again, if it is already sorted. – Chan Mar 27 '15 at 23:13
  • I'm not necessary going to sort the array again, i just want to clarify that i know how to sort an array based on a property of the objects that are in it – Erik Vorcov Mar 27 '15 at 23:14
  • From the error you received, it looks like the objects that are in the array do not have a natural total order, i.e they do not implement the Comparable interface. – Chan Mar 27 '15 at 23:16

2 Answers2

1

A Comparable in Java is an interface that tells Java, "this has a compareTo function". By your context, it looks like you have something like public class Treasure that you want to sort. You'll need to change this to public class Treasure implements Comparable<Treasure> -- see the Comparable docs for more details.

Implementing this will require you to create a compareTo(Treasure other) function, which will let Arrays know how to sort your Treasure objects.

Example code:

public class Treasure implements Comparable<Treasure> {
    private int value;
    public Treasure(int value) { this.value = value; }

    public int compareTo(Treasure other) { return Integer.compare(value, other.value); }

Creating a Comparator--as in @kha's answer--is needed if you wish to use Collections functions that require comparing values. Implementing Comparable--as above--gives your class a public comparator wherever it goes, which can be arguably more useful in certain situations. In your case, you could go either way and be happy.

Community
  • 1
  • 1
xathien
  • 812
  • 7
  • 10
0

From the javadoc of the

public static void sort(Object[] a) 

method --

"Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array)."

You can read more about it here --> http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(java.lang.Object[])

Like I said in my comments, it looks like the array you are passing to your method contains elements that do not have a natural order, i.e they do not implement the Comparable interface.

Here is a sample method declaration for you.

public <K extends Comparable<? super K>> boolean search(K[] values, K key){
}

Personally I think search is not a good method name. I think 'found' would be better.

That said, you might want to read about the Comparable interface here--> http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

Using generics will enforce that the array you pass to the method contains elements that have a natural total order, i.e they implement the Comparable interface.

Note also that the javadoc of the Comparable interface says the following.

"It is strongly recommended (though not required) that natural orderings be consistent with equals. This is so because sorted sets (and sorted maps) without explicit comparators behave "strangely" when they are used with elements (or keys) whose natural ordering is inconsistent with equals. In particular, such a sorted set (or sorted map) violates the general contract for set (or map), which is defined in terms of the equals method."

And hence you may also want to read about the equals and the hashCode methods. Given below is a link of the javadoc of the equals method. http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)

Chan
  • 651
  • 2
  • 8
  • 15