I'm reading a text file into a 2D array. The first column ( the one I'm sorting on) is almost all Doubles. As you all probably know, it sorts 1.1 1.6 25.6 6.4, how can I fix this?
import java.io.*;
import java.util.*;
public class Sort {
public static void main(final String[] args) throws FileNotFoundException {
FileOutputStream out = new FileOutputStream("/Users/evanlivingston/2d.txt");
PrintStream pout = new PrintStream(out);
List<String[]> entries = new ArrayList<String[]>();
Scanner sc = new Scanner(new File("/Users/evanlivingston/dists/0.txt"));
while (sc.hasNext()) {
entries.add(new String[] { sc.next(), sc.next()});
}
String[][] table = entries.toArray(new String[0][]);
Arrays.sort(table, new Comparator<String[]>() {
@Override
public int compare(String[] s1, String[] s2) {
String t1 = s1[0];
String t2 = s2[0];
return t1.compareTo(t2);
}
});
pout.print(java.util.Arrays.deepToString(table));
pout.close();
}
}
EDIT:
Here is my updated code. It won't compile.
import java.io.*;
import java.util.*;
public class Sort {
public static void main(String[] args) throws FileNotFoundException {
FileOutputStream out = new FileOutputStream("/Users/evanlivingston/2d.txt");
PrintStream pout = new PrintStream(out);
List<Coords> coords = new ArrayList<Coords>();
{
Scanner sc = new Scanner(new File("/Users/evanlivingston/dists/0.txt"));
while(sc.hasNextLine()) {
String[] numstrs = sc.nextLine().split("\\s+");
}
String[][] table = coords.toArray(new String[0][]);
Arrays.sort(table, new Comparator<Double[]>() {
@Override
public int compare(Double[] s1, Double[] s2) {
double a = Double.parseDouble(s1);
double b = Double.parseDouble(s2);
compare(a, b);
}
});
pout.print(java.util.Arrays.deepToString(table));
pout.close();
}
}
}