Merge pull request #2566 from nextcloud/localSorting

Fix local sorting
This commit is contained in:
Andy Scherzinger 2018-05-09 16:16:41 +02:00 committed by GitHub
commit 14c586df7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 60 deletions

View file

@ -42,7 +42,7 @@ public class FileSortOrder {
public static final Map<String, FileSortOrder> sortOrders; public static final Map<String, FileSortOrder> sortOrders;
static { static {
sortOrders = new HashMap<String, FileSortOrder>(); sortOrders = new HashMap<>();
sortOrders.put(sort_a_to_z.mName, sort_a_to_z); sortOrders.put(sort_a_to_z.mName, sort_a_to_z);
sortOrders.put(sort_z_to_a.mName, sort_z_to_a); sortOrders.put(sort_z_to_a.mName, sort_z_to_a);
sortOrders.put(sort_old_to_new.mName, sort_old_to_new); sortOrders.put(sort_old_to_new.mName, sort_old_to_new);

View file

@ -1,4 +1,4 @@
/** /*
* Nextcloud Android client application * Nextcloud Android client application
* *
* @author Sven R. Kunze * @author Sven R. Kunze
@ -23,8 +23,6 @@ package com.owncloud.android.utils;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -63,22 +61,16 @@ public class FileSortOrderByDate extends FileSortOrder {
/** /**
* Sorts list by Date. * Sorts list by Date.
* *
* @param filesArray list of files to sort * @param files list of files to sort
*/ */
public File[] sortLocalFiles(File[] filesArray) { public List<File> sortLocalFiles(List<File> files) {
final int multiplier = mAscending ? 1 : -1; final int multiplier = mAscending ? 1 : -1;
List<File> files = new ArrayList<File>(Arrays.asList(filesArray)); Collections.sort(files, (o1, o2) -> {
Long obj1 = o1.lastModified();
Collections.sort(files, new Comparator<File>() { return multiplier * obj1.compareTo(o2.lastModified());
@SuppressFBWarnings(value = "Bx")
public int compare(File o1, File o2) {
Long obj1 = o1.lastModified();
return multiplier * obj1.compareTo(o2.lastModified());
}
}); });
File[] returnArray = new File[files.size()]; return files;
return files.toArray(returnArray);
} }
} }

View file

@ -1,4 +1,4 @@
/** /*
* Nextcloud Android client application * Nextcloud Android client application
* *
* @author Sven R. Kunze * @author Sven R. Kunze
@ -23,8 +23,6 @@ package com.owncloud.android.utils;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -70,30 +68,25 @@ public class FileSortOrderByName extends FileSortOrder {
/** /**
* Sorts list by Name. * Sorts list by Name.
* *
* @param filesArray files to sort * @param files files to sort
*/ */
public File[] sortLocalFiles(File[] filesArray) { public List<File> sortLocalFiles(List<File> files) {
final int multiplier = mAscending ? 1 : -1; final int multiplier = mAscending ? 1 : -1;
List<File> files = new ArrayList<>(Arrays.asList(filesArray)); Collections.sort(files, (o1, o2) -> {
if (o1.isDirectory() && o2.isDirectory()) {
Collections.sort(files, new Comparator<File>() { return multiplier * o1.getPath().toLowerCase(Locale.getDefault())
public int compare(File o1, File o2) { .compareTo(o2.getPath().toLowerCase(Locale.getDefault()));
if (o1.isDirectory() && o2.isDirectory()) { } else if (o1.isDirectory()) {
return multiplier * o1.getPath().toLowerCase(Locale.getDefault()) return -1;
.compareTo(o2.getPath().toLowerCase(Locale.getDefault())); } else if (o2.isDirectory()) {
} else if (o1.isDirectory()) { return 1;
return -1;
} else if (o2.isDirectory()) {
return 1;
}
return multiplier * new AlphanumComparator().compare(o1.getPath()
.toLowerCase(Locale.getDefault()),
o2.getPath().toLowerCase(Locale.getDefault()));
} }
return multiplier * new AlphanumComparator().compare(o1.getPath()
.toLowerCase(Locale.getDefault()),
o2.getPath().toLowerCase(Locale.getDefault()));
}); });
File[] returnArray = new File[files.size()]; return files;
return files.toArray(returnArray);
} }
} }

View file

@ -23,8 +23,6 @@ package com.owncloud.android.utils;
import com.owncloud.android.datamodel.OCFile; import com.owncloud.android.datamodel.OCFile;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -73,32 +71,26 @@ public class FileSortOrderBySize extends FileSortOrder {
/** /**
* Sorts list by Size. * Sorts list by Size.
* *
* @param filesArray list of files to sort * @param files list of files to sort
*/ */
public File[] sortLocalFiles(File[] filesArray) { public List<File> sortLocalFiles(List<File> files) {
final int multiplier = mAscending ? 1 : -1; final int multiplier = mAscending ? 1 : -1;
List<File> files = new ArrayList<>(Arrays.asList(filesArray)); Collections.sort(files, (o1, o2) -> {
if (o1.isDirectory() && o2.isDirectory()) {
Collections.sort(files, new Comparator<File>() { Long obj1 = FileStorageUtils.getFolderSize(o1);
@SuppressFBWarnings(value = "Bx") return multiplier * obj1.compareTo(FileStorageUtils.getFolderSize(o2));
public int compare(File o1, File o2) { } else if (o1.isDirectory()) {
if (o1.isDirectory() && o2.isDirectory()) { return -1;
Long obj1 = FileStorageUtils.getFolderSize(o1); } else if (o2.isDirectory()) {
return multiplier * obj1.compareTo(FileStorageUtils.getFolderSize(o2)); return 1;
} else if (o1.isDirectory()) { } else {
return -1; Long obj1 = o1.length();
} else if (o2.isDirectory()) { return multiplier * obj1.compareTo(o2.length());
return 1;
} else {
Long obj1 = o1.length();
return multiplier * obj1.compareTo(o2.length());
}
} }
}); });
File[] returnArray = new File[files.size()]; return files;
return files.toArray(returnArray);
} }
} }