Skip to content

Commit

Permalink
Merge pull request #386 from Logan676/sort_files
Browse files Browse the repository at this point in the history
Sort files (or repos) correctly
  • Loading branch information
Logan676 committed Jul 6, 2015
2 parents a0d7973 + 50009f9 commit 9763871
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/com/seafile/seadroid2/data/SeafDirent.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static class DirentNameComparator implements Comparator<SeafDirent> {

@Override
public int compare(SeafDirent itemA, SeafDirent itemB) {
return itemB.name.toLowerCase().compareTo(itemA.name.toLowerCase());
return itemA.name.toLowerCase().compareTo(itemB.name.toLowerCase());
}
}
}
36 changes: 6 additions & 30 deletions src/com/seafile/seadroid2/data/SeafGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,40 +44,16 @@ public void addIfAbsent(SeafRepo repo) {
*/
public void sortByType(int type, int order) {
if (type == SeafItemAdapter.SORT_BY_NAME) {
if (order == SeafItemAdapter.SORT_ORDER_ASCENDING) {
Collections.sort(repos, new RepoNameComparator());
} else if (order == SeafItemAdapter.SORT_ORDER_DESCENDING) {
Collections.sort(repos, Collections.reverseOrder(new RepoNameComparator()));
Collections.sort(repos, new SeafRepo.RepoNameComparator());
if (order == SeafItemAdapter.SORT_ORDER_DESCENDING) {
Collections.reverse(repos);
}
} else if (type == SeafItemAdapter.SORT_BY_LAST_MODIFIED_TIME) {
if (order == SeafItemAdapter.SORT_ORDER_ASCENDING) {
Collections.sort(repos, new RepoLastMTimeComparator());
} else if (order == SeafItemAdapter.SORT_ORDER_DESCENDING) {
Collections.sort(repos, Collections.reverseOrder(new RepoLastMTimeComparator()));
Collections.sort(repos, new SeafRepo.RepoLastMTimeComparator());
if (order == SeafItemAdapter.SORT_ORDER_DESCENDING) {
Collections.reverse(repos);
}
}
}

/**
* Repository last modified time comparator class
*/
private class RepoLastMTimeComparator implements Comparator<SeafRepo> {

@Override
public int compare(SeafRepo itemA, SeafRepo itemB) {
return (int) (itemB.mtime - itemA.mtime);
}
}

/**
* Repository name comparator class
*/
private class RepoNameComparator implements Comparator<SeafRepo> {

@Override
public int compare(SeafRepo itemA, SeafRepo itemB) {
return itemA.name.toLowerCase().compareTo(itemB.name.toLowerCase());
}
}

}
23 changes: 23 additions & 0 deletions src/com/seafile/seadroid2/data/SeafRepo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.seafile.seadroid2.data;

import java.util.Comparator;
import java.util.Date;

import android.util.Log;
Expand Down Expand Up @@ -83,4 +84,26 @@ public int getIcon() {
public boolean hasWritePermission() {
return permission.indexOf('w') != -1;
}

/**
* Repository last modified time comparator class
*/
public static class RepoLastMTimeComparator implements Comparator<SeafRepo> {

@Override
public int compare(SeafRepo itemA, SeafRepo itemB) {
return (int) (itemA.mtime - itemB.mtime);
}
}

/**
* Repository name comparator class
*/
public static class RepoNameComparator implements Comparator<SeafRepo> {

@Override
public int compare(SeafRepo itemA, SeafRepo itemB) {
return itemA.name.toLowerCase().compareTo(itemB.name.toLowerCase());
}
}
}
25 changes: 13 additions & 12 deletions src/com/seafile/seadroid2/ui/adapter/SeafItemAdapter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seafile.seadroid2.ui.adapter;

import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
Expand Down Expand Up @@ -625,20 +626,20 @@ public void sortFiles(int type, int order) {

// sort SeafDirents
if (type == SORT_BY_NAME) {
if (order == SORT_ORDER_ASCENDING) {
Collections.sort(folders, new SeafDirent.DirentNameComparator());
Collections.sort(files, new SeafDirent.DirentNameComparator());
} else if (order == SORT_ORDER_DESCENDING) {
Collections.sort(folders, Collections.reverseOrder(new SeafDirent.DirentNameComparator()));
Collections.sort(files, Collections.reverseOrder(new SeafDirent.DirentNameComparator()));
// sort by name, in ascending order
Collections.sort(folders, new SeafDirent.DirentNameComparator());
Collections.sort(files, new SeafDirent.DirentNameComparator());
if (order == SORT_ORDER_DESCENDING) {
Collections.reverse(folders);
Collections.reverse(files);
}
} else if (type == SORT_BY_LAST_MODIFIED_TIME) {
if (order == SORT_ORDER_ASCENDING) {
Collections.sort(folders, new SeafDirent.DirentLastMTimeComparator());
Collections.sort(files, new SeafDirent.DirentLastMTimeComparator());
} else if (order == SORT_ORDER_DESCENDING) {
Collections.sort(folders, Collections.reverseOrder(new SeafDirent.DirentLastMTimeComparator()));
Collections.sort(files, Collections.reverseOrder(new SeafDirent.DirentLastMTimeComparator()));
// sort by last modified time, in ascending order
Collections.sort(folders, new SeafDirent.DirentLastMTimeComparator());
Collections.sort(files, new SeafDirent.DirentLastMTimeComparator());
if (order == SORT_ORDER_DESCENDING) {
Collections.reverse(folders);
Collections.reverse(files);
}
}
// Adds the objects in the specified collection to this ArrayList
Expand Down

0 comments on commit 9763871

Please sign in to comment.