Use switch... and effectively sorts before listing.

Change-Id: Ia7eb2c53dc8a69b3d65e56afc7a27f0548c63d07
This commit is contained in:
Siqi Liu 2015-04-23 15:18:23 +02:00
parent dde1964a2c
commit 0848e70b0f
2 changed files with 52 additions and 50 deletions

View File

@ -10,11 +10,14 @@ package org.libreoffice.ui;
import org.libreoffice.R;
import org.libreoffice.storage.IFile;
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.util.Map;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.HashMap;
import java.util.Comparator;
import android.util.Log;
@ -198,55 +201,52 @@ public class FileUtilities {
};
}
static void sortFiles(File[] files, int sortMode) {
// Should really change all this to a switch statement...
if (sortMode == SORT_AZ) {
Arrays.sort(files , new Comparator<File>() {
public int compare(File lhs, File rhs) {
return lhs.getName().compareTo(rhs.getName());
}
});
return;
}
if (sortMode == SORT_ZA) {
Arrays.sort(files , new Comparator<File>() {
public int compare(File lhs, File rhs) {
return rhs.getName().compareTo(lhs.getName());
}
});
return;
}
if (sortMode == SORT_OLDEST) {
Arrays.sort(files , new Comparator<File>() {
public int compare(File lhs, File rhs) {
return Long.valueOf(lhs.lastModified()).compareTo(rhs.lastModified());
}
});
return;
}
if (sortMode == SORT_NEWEST) {
Arrays.sort(files , new Comparator<File>() {
public int compare(File lhs, File rhs) {
return Long.valueOf(rhs.lastModified()).compareTo(lhs.lastModified());
}
});
return;
}
if (sortMode == SORT_LARGEST) {
Arrays.sort(files , new Comparator<File>() {
public int compare(File lhs, File rhs) {
return Long.valueOf(rhs.length()).compareTo(lhs.length());
}
});
return;
}
if (sortMode == SORT_SMALLEST) {
Arrays.sort(files , new Comparator<File>() {
public int compare(File lhs, File rhs) {
return Long.valueOf(lhs.length()).compareTo(rhs.length());
}
});
return;
static void sortFiles(List<IFile> files, int sortMode) {
switch (sortMode) {
case SORT_AZ:
Collections.sort(files , new Comparator<IFile>() {
public int compare(IFile lhs, IFile rhs) {
return lhs.getName().compareTo(rhs.getName());
}
});
break;
case SORT_ZA:
Collections.sort(files , new Comparator<IFile>() {
public int compare(IFile lhs, IFile rhs) {
return rhs.getName().compareTo(lhs.getName());
}
});
break;
case SORT_OLDEST:
Collections.sort(files , new Comparator<IFile>() {
public int compare(IFile lhs, IFile rhs) {
return lhs.getLastModified().compareTo(rhs.getLastModified());
}
});
break;
case SORT_NEWEST:
Collections.sort(files , new Comparator<IFile>() {
public int compare(IFile lhs, IFile rhs) {
return rhs.getLastModified().compareTo(lhs.getLastModified());
}
});
break;
case SORT_LARGEST:
Collections.sort(files , new Comparator<IFile>() {
public int compare(IFile lhs, IFile rhs) {
return Long.valueOf(rhs.getSize()).compareTo(lhs.getSize());
}
});
break;
case SORT_SMALLEST:
Collections.sort(files , new Comparator<IFile>() {
public int compare(IFile lhs, IFile rhs) {
return Long.valueOf(lhs.getSize()).compareTo(rhs.getSize());
}
});
break;
default:
Log.e(LOGTAG, "uncatched sortMode: " + sortMode);
}
return;
}

View File

@ -177,6 +177,8 @@ public class LibreOfficeUIActivity extends ActionBarActivity implements ActionBa
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
FileUtilities.sortFiles(filePaths, sortMode);
// refresh view
if (viewMode == GRID_VIEW) {
gv.setAdapter(new GridItemAdapter(getApplicationContext(),