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?

seeing as to multiple people viewed and not replied and highly doubting they do not know it must be something silly :|

Are you comparing on the file size? If so, you convert it to String, and do a String compareTo so, for example "1000000"sorts lower than "2".

ps Any special reason why you use a 2D array? Why not just keep the array of Files and use File's accessor methods when you need values?

ah, so it seems i have the options of either creating an array of objects or keep the existing string array and parse the string elements.

doing

return Long.parseLong(str1[1]).compareTo(Long.parseLong(str2[1]));

got me a

Cannot invoke compareTo(long) on the primitive type long

any input on that?

any input on that?

It's (in my opinion) one of Java's poorest design points - primitive types that are not Objects, and so need an Object equivalent to get all the functionality. To call a Long method you need a Long object, not a long primitive. Since you know that the String is a valid representation of a Long, you can safely use

new Long(str1[1]).compareTo(new Long(str2[1]));

thanks for your comments. not only did they help me solve the excecise, they also put a much more clearly distinguishable line between a primitive and an object.

This question has already been answered. Start a new discussion instead.