I am having difficulty with a sorting routine. I believe that the concept is valid (although not necessarily the most efficient), but I keep running into a problem. I am trying to use the compareTo function to identify the relationship between two values in an array, but it seems to have an issue with it being a comparison of two float values.
gives the compile error as follows:
for (int x = 0; x < 430; x++)
for (int y = 0; y < 430; y++)
if (dataArray[y].compareTo(dataArray[y + 1]) > 0);
tempOpen = dataArray[y];
dataArray[y] = dataArray[y + 1];
dataArray[y + 1] = tempOpen;
tempDate = dateArray[y];
dateArray[y] = dateArray [y + 1];
dateArray [y + 1] = tempDate;
File: C:\Users\Brad\Downloads\Assignment 3\Calculations.java [line: 157]
Error: Cannot invoke compareTo(float) on the array type float
Any help would be appreciated.
Aren't your arrays 2D? Thats what compareTo is complaining about. You are passing an instance of a 1D array.
Thanks Jim. Not sure how I missed that. Unfortunately now it is giving me a slightly different error
Cannot invoke compareTo(float) on the primitive type float
Primitives (such as floats) don't have methods; you can and have to compare them with the ordinary arithmetic operators.
Look at the Arrays class. It has methods for sorting arrays of primitives.