Comparing multidimensional arrays

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 76
Reputation: Lensva has a little shameless behaviour in the past 
Solved Threads: 3
Lensva Lensva is offline Offline
Junior Poster in Training

Comparing multidimensional arrays

 
0
  #1
Jul 17th, 2009
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
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class DirScan {
  5.  
  6. public static void main(String[] args) {
  7. File rootDir = new File(System.getProperty("user.dir")),
  8. list[] = rootDir.listFiles();
  9.  
  10. String infoHolder[][] = new String[list.length][3];
  11.  
  12. for(int i=0; i<list.length; i++) {
  13. infoHolder[i][0] = list[i].getName();
  14. infoHolder[i][1] = Long.toString(list[i].length());
  15. infoHolder[i][2] = Long.toString(list[i].lastModified());
  16. }
  17.  
  18. Arrays.sort(infoHolder, new Compare());
  19.  
  20.  
  21. System.out.printf("%20s%20s%20s\n","name","size","modified");
  22. for(String[] a:infoHolder) {
  23. for(String b:a) System.out.printf("%20s",b);
  24. System.out.println();
  25. }
  26. }
  27. }
  28.  
  29. class Compare implements Comparator {
  30.  
  31. public int compare(Object obj1, Object obj2) {
  32.  
  33. String[] str1 = (String[])obj1,
  34. str2 = (String[])obj2;
  35. //should now list by size
  36. return str1[1].compareTo(str2[1]);
  37. }
  38.  
  39. }
also, any general tips regarding excercise realization?
Last edited by Lensva; Jul 17th, 2009 at 9:19 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 76
Reputation: Lensva has a little shameless behaviour in the past 
Solved Threads: 3
Lensva Lensva is offline Offline
Junior Poster in Training

Re: Comparing multidimensional arrays

 
0
  #2
Jul 17th, 2009
seeing as to multiple people viewed and not replied and highly doubting they do not know it must be something silly
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 989
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is online now Online
Posting Shark

Re: Comparing multidimensional arrays

 
0
  #3
Jul 17th, 2009
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?
Last edited by JamesCherrill; Jul 17th, 2009 at 12:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 76
Reputation: Lensva has a little shameless behaviour in the past 
Solved Threads: 3
Lensva Lensva is offline Offline
Junior Poster in Training

Re: Comparing multidimensional arrays

 
0
  #4
Jul 17th, 2009
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
  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 989
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 146
JamesCherrill JamesCherrill is online now Online
Posting Shark

Re: Comparing multidimensional arrays

 
0
  #5
Jul 17th, 2009
Originally Posted by Lensva View Post
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]));
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 76
Reputation: Lensva has a little shameless behaviour in the past 
Solved Threads: 3
Lensva Lensva is offline Offline
Junior Poster in Training

Re: Comparing multidimensional arrays

 
0
  #6
Jul 17th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC