[JAVA] File 배열을 파일명 순으로 정렬 : sortFileArray
File객체 배열(File[])을 파일명 순으로 정렬하는 메서드
———-
private void sortFileArray(File[] fileArr) throws Exception {
if (fileArr == null) {
return;
}
if (fileArr.length < 2) {
return;
}
File file1 = null;
File file2 = null;
File tempFile = null;
int fileCount = fileArr.length;
for (int i=0; i<fileCount; i++) {
for (int k=i+1; k<fileCount; k++) {
file1 = fileArr[i];
file2 = fileArr[k];
if (checkShouldChangeOrder(file1.getName(), file2.getName())) {
tempFile = fileArr[i];
fileArr[i] = file2;
fileArr[k] = tempFile;
}
}
}
}
private boolean checkShouldChangeOrder(String firstStr, String nextStr) {
if (firstStr == null) {
firstStr = “”;
}
if (nextStr == null) {
nextStr = “”;
}
if (firstStr.length() > nextStr.length()) {
return true;
} else if (firstStr.length() < nextStr.length()) {
return false;
}
char ch1 = 0;
char ch2 = 0;
int len = firstStr.length();
for (int i=0; i<len; i++) {
ch1 = firstStr.charAt(i);
ch2 = nextStr.charAt(i);
if (ch1 == ch2) {
continue;
} else if (ch1 > ch2) {
return true;
} else if (ch1 < ch2) {
return false;
}
}
return false;
}