User:Andy Maloney/Notebook/Lab Notebook of Andy Maloney/2011/04/21/Learning how to sort in Java: Difference between revisions
Andy Maloney (talk | contribs) (New page: {{AndyMaloneyNotebook |Description=Continuation from yesterday on learning how to sort in Java. }} Category:AM_Java Category:AM_Tobacco seeds ==Sorting== ===Sorting with names=== ...) |
Andy Maloney (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
==Reexamining yesterday's code== | |||
<source lang="Java"> | <source lang="Java"> | ||
import java.io.*; | |||
import java.util.*; | import java.util.*; | ||
public class | public class SortListFiles { | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
String path = "/home/andy/Documents/TobaccoSeeds/Cam6/"; | |||
String | File tempFile = new File(path); | ||
File[] listOfFileNames = tempFile.listFiles(); | |||
int arrayLength = listOfFileNames.length; | |||
String[] fileNameArray = new String[arrayLength]; | |||
String[] | |||
for(int i = 0; i < arrayLength; i++) { | |||
if(listOfFileNames[i].isFile()) { | |||
fileNameArray[i] = listOfFileNames[i].getName(); | |||
} | |||
} | |||
Arrays.sort(fileNameArray); | |||
System.out.println(Arrays.toString(fileNameArray)); | |||
} | } | ||
} | } | ||
</source> | </source> | ||
So, I am not getting the files in lexicographical order with the above code. I have a list that goes from ''Cam6-1000.jpg'' to ''Cam6-10000.jpg''. Changing the Arrays.sort to include the Collator.getInstance does not help either. | |||
Okay, so from what I understand, I now need to a function that compares the list values and arranges them in order dependent on their comparisons. | |||
Hmm, I can't seem to get it... | |||
I also looked at the sorting code found at the top of the page and changed it some what. | |||
<source lang="Java"> | <source lang="Java"> | ||
import java.util.*; | import java.util.*; | ||
import java.text.*; | import java.text.*; | ||
public class Sorting { | public class Sorting { | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
Line 122: | Line 38: | ||
Integer[] B = {0, 48, 400, 55, 557, 16, 1677, 1, 10, 66, 87123, 6458795}; | Integer[] B = {0, 48, 400, 55, 557, 16, 1677, 1, 10, 66, 87123, 6458795}; | ||
String[] C = {"0001", "0100", "1000", "2010", "1234", "0004", "23956", "2395"}; | String[] C = {"0001", "0100", "1000", "2010", "1234", "0004", "23956", "2395"}; | ||
String[] D = {"Cam6-0001.jpg", "Cam6-10000.jpg", "Cam6-346722.jpg", "Cam6-0042.jpg", "Cam6-1006", "Cam6-10064"}; | |||
System.out.println("A: " + Arrays.toString(A)); | System.out.println("A: " + Arrays.toString(A)); | ||
Arrays.sort(A, Collator.getInstance()); | Arrays.sort(A, Collator.getInstance()); | ||
System.out.println("A sorted: " + Arrays.toString(A)); | System.out.println("A sorted: " + Arrays.toString(A)); | ||
System.out.println(); | System.out.println(); | ||
System.out.println("B: " + Arrays.toString(B)); | System.out.println("B: " + Arrays.toString(B)); | ||
Arrays.sort(B); | Arrays.sort(B); | ||
System.out.println("B sorted: " + Arrays.toString(B)); | System.out.println("B sorted: " + Arrays.toString(B)); | ||
System.out.println(); | System.out.println(); | ||
System.out.println("C: " + Arrays.toString(C)); | System.out.println("C: " + Arrays.toString(C)); | ||
Arrays.sort(C); | Arrays.sort(C); | ||
System.out.println("C sorted: " + Arrays.toString(C)); | System.out.println("C sorted: " + Arrays.toString(C)); | ||
System.out.println(); | System.out.println(); | ||
System.out.println("D: " + Arrays.toString(D)); | System.out.println("D: " + Arrays.toString(D)); | ||
Line 191: | Line 61: | ||
} | } | ||
</source> | </source> | ||
The output shows that this sorting isn't working. | |||
<code> | <code> | ||
Line 209: | Line 79: | ||
D: [Cam6-0001.jpg, Cam6-10000.jpg, Cam6-346722.jpg, Cam6-0042.jpg] | D: [Cam6-0001.jpg, Cam6-10000.jpg, Cam6-346722.jpg, Cam6-0042.jpg, Cam6-1006, Cam6-10064] | ||
D sorted: [Cam6-0001.jpg, Cam6-0042.jpg, Cam6-10000.jpg, Cam6-346722.jpg] | D sorted: [Cam6-0001.jpg, Cam6-0042.jpg, Cam6-10000.jpg, Cam6-1006, Cam6-10064, Cam6-346722.jpg] | ||
</code> | </code> | ||
Now I'm really confused. | |||
Revision as of 12:09, 21 April 2011
Reexamining yesterday's code
<source lang="Java"> import java.io.*; import java.util.*;
public class SortListFiles { public static void main(String[] args) { String path = "/home/andy/Documents/TobaccoSeeds/Cam6/"; File tempFile = new File(path); File[] listOfFileNames = tempFile.listFiles(); int arrayLength = listOfFileNames.length; String[] fileNameArray = new String[arrayLength];
for(int i = 0; i < arrayLength; i++) { if(listOfFileNames[i].isFile()) { fileNameArray[i] = listOfFileNames[i].getName(); } } Arrays.sort(fileNameArray); System.out.println(Arrays.toString(fileNameArray)); } } </source> So, I am not getting the files in lexicographical order with the above code. I have a list that goes from Cam6-1000.jpg to Cam6-10000.jpg. Changing the Arrays.sort to include the Collator.getInstance does not help either.
Okay, so from what I understand, I now need to a function that compares the list values and arranges them in order dependent on their comparisons.
Hmm, I can't seem to get it...
I also looked at the sorting code found at the top of the page and changed it some what. <source lang="Java"> import java.util.*; import java.text.*;
public class Sorting { public static void main(String[] args) { String[] A = {"Andy", "andy", "Steve", "steve", "Anthony", "anthony", "Nadia", "nadia", "Pranav", "pranav", "Brian", "brian"}; Integer[] B = {0, 48, 400, 55, 557, 16, 1677, 1, 10, 66, 87123, 6458795}; String[] C = {"0001", "0100", "1000", "2010", "1234", "0004", "23956", "2395"}; String[] D = {"Cam6-0001.jpg", "Cam6-10000.jpg", "Cam6-346722.jpg", "Cam6-0042.jpg", "Cam6-1006", "Cam6-10064"};
System.out.println("A: " + Arrays.toString(A)); Arrays.sort(A, Collator.getInstance()); System.out.println("A sorted: " + Arrays.toString(A));
System.out.println(); System.out.println("B: " + Arrays.toString(B)); Arrays.sort(B); System.out.println("B sorted: " + Arrays.toString(B));
System.out.println(); System.out.println("C: " + Arrays.toString(C)); Arrays.sort(C); System.out.println("C sorted: " + Arrays.toString(C));
System.out.println(); System.out.println("D: " + Arrays.toString(D)); Arrays.sort(D); System.out.println("D sorted: " + Arrays.toString(D)); } } </source> The output shows that this sorting isn't working.
A: [Andy, andy, Steve, steve, Anthony, anthony, Nadia, nadia, Pranav, pranav, Brian, brian]
A sorted: [andy, Andy, anthony, Anthony, brian, Brian, nadia, Nadia, pranav, Pranav, steve, Steve]
B: [0, 48, 400, 55, 557, 16, 1677, 1, 10, 66, 87123, 6458795]
B sorted: [0, 1, 10, 16, 48, 55, 66, 400, 557, 1677, 87123, 6458795]
C: [0001, 0100, 1000, 2010, 1234, 0004, 23956, 2395]
C sorted: [0001, 0004, 0100, 1000, 1234, 2010, 2395, 23956]
D: [Cam6-0001.jpg, Cam6-10000.jpg, Cam6-346722.jpg, Cam6-0042.jpg, Cam6-1006, Cam6-10064]
D sorted: [Cam6-0001.jpg, Cam6-0042.jpg, Cam6-10000.jpg, Cam6-1006, Cam6-10064, Cam6-346722.jpg]
Now I'm really confused.