Skip to content

Commit

Permalink
Use Collators to make sure special characters are sorted correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
daneren2005 committed Mar 16, 2016
1 parent a9fd0ce commit 1bd7596
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
16 changes: 6 additions & 10 deletions app/src/main/java/github/daneren2005/dsub/domain/Artist.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import android.util.Log;

import java.io.Serializable;
import java.text.Collator;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

/**
* @author Sindre Mehus
Expand Down Expand Up @@ -119,9 +121,12 @@ public String toString() {

public static class ArtistComparator implements Comparator<Artist> {
private String[] ignoredArticles;
private Collator collator;

public ArtistComparator(String[] ignoredArticles) {
this.ignoredArticles = ignoredArticles;
this.collator = Collator.getInstance(Locale.US);
this.collator.setStrength(Collator.PRIMARY);
}

public int compare(Artist lhsArtist, Artist rhsArtist) {
Expand All @@ -134,15 +139,6 @@ public int compare(Artist lhsArtist, Artist rhsArtist) {
String lhs = lhsArtist.getName().toLowerCase();
String rhs = rhsArtist.getName().toLowerCase();

char lhs1 = lhs.charAt(0);
char rhs1 = rhs.charAt(0);

if (Character.isDigit(lhs1) && !Character.isDigit(rhs1)) {
return -1;
} else if (Character.isDigit(rhs1) && !Character.isDigit(lhs1)) {
return 1;
}

for (String article : ignoredArticles) {
int index = lhs.indexOf(article.toLowerCase() + " ");
if (index == 0) {
Expand All @@ -154,7 +150,7 @@ public int compare(Artist lhsArtist, Artist rhsArtist) {
}
}

return lhs.compareTo(rhs);
return collator.compare(lhs, rhs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.util.Log;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.File;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.Locale;

import github.daneren2005.dsub.service.DownloadService;
import github.daneren2005.dsub.util.Constants;
Expand Down Expand Up @@ -617,9 +620,12 @@ public String toString() {

public static class EntryComparator implements Comparator<Entry> {
private boolean byYear;
private Collator collator;

public EntryComparator(boolean byYear) {
this.byYear = byYear;
this.collator = Collator.getInstance(Locale.US);
this.collator.setStrength(Collator.PRIMARY);
}

public int compare(Entry lhs, Entry rhs) {
Expand All @@ -639,8 +645,8 @@ public int compare(Entry lhs, Entry rhs) {
return 1;
}
}
return lhs.getAlbumDisplay().compareToIgnoreCase(rhs.getAlbumDisplay());

return collator.compare(lhs.getAlbumDisplay(), rhs.getAlbumDisplay());
}

Integer lhsDisc = lhs.getDiscNumber();
Expand All @@ -664,7 +670,7 @@ public int compare(Entry lhs, Entry rhs) {
return 1;
}

return lhs.getTitle().compareToIgnoreCase(rhs.getTitle());
return collator.compare(lhs.getTitle(), rhs.getTitle());
}

public static void sort(List<Entry> entries) {
Expand Down

0 comments on commit 1bd7596

Please sign in to comment.