as far as i understand in order to sort a multidimensional array you have to make you own class that implements Comparator.
the string which is to be sorted has the following structure:
[0][0] filename1 [0][1]filesize1 [0][2]lastmodified1
[1][0] filename2 [1][1]filesize2 [1][2]lastmodified2
...
i want to be able to compare by an element of choice. at the moment class Sort does sort, however not as intended
import java.io.*;
import java.util.*;
public class DirScan {
public static void main(String[] args) {
File rootDir = new File(System.getProperty("user.dir")),
list[] = rootDir.listFiles();
String infoHolder[][] = new String[list.length][3];
for(int i=0; i<list.length; i++) {
infoHolder[i][0] = list[i].getName();
infoHolder[i][1] = Long.toString(list[i].length());
infoHolder[i][2] = Long.toString(list[i].lastModified());
}
Arrays.sort(infoHolder, new Compare());
System.out.printf("%20s%20s%20s\n","name","size","modified");
for(String[] a:infoHolder) {
for(String b:a) System.out.printf("%20s",b);
System.out.println();
}
}
}
class Compare implements Comparator {
public int compare(Object obj1, Object obj2) {
String[] str1 = (String[])obj1,
str2 = (String[])obj2;
//should now list by size
return str1[1].compareTo(str2[1]);
}
}
also, any general tips regarding excercise realization?