Reputation: 33
I'm trying to sort data in an array that looks like this:
(String, int, String, double)
or
(David, 25, Da, 123.54)
I want the delineation for the array sort to be decided by the "integer" value. So, for example:
David, 25, Da, 123.54
Sean, 27, Pa, 514.21
Luke, 32, Ma, 221.54
These values are being read from a file, organized in a constructor,and sent back to the driver.
Here's what I have so far, I'm not sure of how much use it will be:
public class NamesAges{
public String[] display() throws IOException {
FileReader fileReader = new FileReader("elements.csv");
BufferedReader bufferedReader = new BufferedReader(fileReader);
List<String> lines = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null)
{
lines.add(line);
System.out.println(line);
}
bufferedReader.close();
String[] sl = (String[]) lines.toArray(new String[0]);
return sl;
}
}
Any tips?
Upvotes: 1
Views: 102
Reputation: 308848
I'd recommend encapsulating each row into an Object
and writing a Comparator
to sort them as needed. Java's an object-oriented language; stop thinking in terms of primitives and Strings.
Upvotes: 4
Reputation: 3909
The easiest way would probably be to create a class that encapsulates the four values, make the class implement Comparable
and define its compareTo()
method via the integer.
Upvotes: 2