From 857ca6b21dbdd7b2633303a0bae08dedb1dd0446 Mon Sep 17 00:00:00 2001 From: daneren2005 Date: Fri, 15 Nov 2013 16:30:59 -0800 Subject: Added ability to do sort by year in EntryComparator --- .../daneren2005/dsub/domain/MusicDirectory.java | 31 +++++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/github/daneren2005/dsub/domain/MusicDirectory.java b/src/github/daneren2005/dsub/domain/MusicDirectory.java index 0b9be5fb..724beed2 100644 --- a/src/github/daneren2005/dsub/domain/MusicDirectory.java +++ b/src/github/daneren2005/dsub/domain/MusicDirectory.java @@ -96,7 +96,10 @@ public class MusicDirectory implements Serializable { } public void sortChildren() { - EntryComparator.sort(children); + EntryComparator.sort(children, false); + } + public void sortChildren(boolean sortByYear) { + EntryComparator.sort(children, sortByYear); } public static class Entry implements Serializable { @@ -364,13 +367,33 @@ public class MusicDirectory implements Serializable { } public static class EntryComparator implements Comparator { + private boolean sortByYear = false; + + EntryComparator(boolean byYear) { + sortByYear = byYear; + } + public int compare(Entry lhs, Entry rhs) { if(lhs.isDirectory() && !rhs.isDirectory()) { return -1; } else if(!lhs.isDirectory() && rhs.isDirectory()) { return 1; } else if(lhs.isDirectory() && rhs.isDirectory()) { - return lhs.getTitle().compareToIgnoreCase(rhs.getTitle()); + if(sortByYear) { + Integer lhsYear = lhs.getYear(); + Integer rhsYear = rhs.getYear(); + if(lhsYear != null && rhsYear != null) { + return lhsYear.compareTo(rhsYear); + } else if(lhsYear != null) { + return -1; + } else if(rhsYear != null) { + return 1; + } else { + return lhs.getTitle().compareToIgnoreCase(rhs.getTitle()); + } + } else { + return lhs.getTitle().compareToIgnoreCase(rhs.getTitle()); + } } Integer lhsDisc = lhs.getDiscNumber(); @@ -397,9 +420,9 @@ public class MusicDirectory implements Serializable { return lhs.getTitle().compareToIgnoreCase(rhs.getTitle()); } - public static void sort(List entries) { + public static void sort(List entries, boolean sortByYear) { try { - Collections.sort(entries, new EntryComparator()); + Collections.sort(entries, new EntryComparator(sortByYear)); } catch (Exception e) { Log.w(TAG, "Failed to sort MusicDirectory"); } -- cgit v1.2.3