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;
static {
sortOrders = new HashMap<String, FileSortOrder>();
sortOrders = new HashMap<>();
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_old_to_new.mName, sort_old_to_new);

View file

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

View file

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

View file

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