| | |
Comparing multidimensional arrays
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2008
Posts: 76
Reputation:
Solved Threads: 3
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
also, any general tips regarding excercise realization?
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
java Syntax (Toggle Plain Text)
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]); } }
Last edited by Lensva; Jul 17th, 2009 at 9:19 am.
•
•
Join Date: Apr 2008
Posts: 989
Reputation:
Solved Threads: 146
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?
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?
Last edited by JamesCherrill; Jul 17th, 2009 at 12:06 pm.
•
•
Join Date: Mar 2008
Posts: 76
Reputation:
Solved Threads: 3
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 got me a any input on that?
doing
java Syntax (Toggle Plain Text)
return Long.parseLong(str1[1]).compareTo(Long.parseLong(str2[1]));
•
•
•
•
Cannot invoke compareTo(long) on the primitive type long
•
•
Join Date: Apr 2008
Posts: 989
Reputation:
Solved Threads: 146
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]));
new Long(str1[1]).compareTo(new Long(str2[1]));
![]() |
Similar Threads
- (reformatted) How to return Multi-Dimensional Arrays (C++)
- Passing Multidimensional Arrays To Functions (C++)
- Initializing Multidimensional Arrays plz help! (C++)
- What relation does **indirection operator have with Multidimensional Arrays (C++)
- Java Multidimensional Arrays (Java)
- Zend PHP Certification (PHP)
- How to Return Multidimensional Arrays (C++)
Other Threads in the Java Forum
- Previous Thread: Help with my array
- Next Thread: How can I know if the site is accessed by a mobile browser?
| Thread Tools | Search this Thread |
actuate android api applet application array arrays automation balls binary bluetooth bold business c++ chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging draw ebook eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui hql html ide image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie nextline numbers oracle parameter php print problem program programming project recursion recursive scanner screen server set size sms sort sql string sun swing swt threads time tree user websites windows





