So how to find the minimum in an array of objects?

Xylitol

Diamond Member
Aug 28, 2005
6,617
0
76
My code is this:

public Comparable minimum(Comparable[] inArray)
{
Comparable result = inArray[0];
for(int i = 1; i < inArray.length; i++)
{
if(inArray.compareTo(result) < 1)
{
result = inArray;
}
}
return result;
}

What I want to do is in an array of both doubles / strings (words), I want to find the smallest # in the double array and the first word (in alphabetical order) in the string array

Thanks
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Might want to use attach code if you're going to use [ i ]

Your problem statement is fuzzy.

"want to find the smallest # in the double array and the first word (in alphabetical order) in the string array "

So "Comparable" has both string and double elements?

Given that the element of your array containing the smallest double won't always contain the first string I assume you plan to copy the individual pieces of different array elements to form a new "Comparable"?

And you don't need to know the 2 index values of the array elements that you copied from, you just want your composite "Comparable"?

If the above is correct, what approach do you plan to take?

(Hopefully no one will be stupid enough to just write it for you, since that defeats the purpose of the assignment.)