public class Leaderboard extends RealmObject {
String score;
}
RealmResults<Leaderboard> leaderboardList = realm.where(Leaderboard.class).distinct("score").findAll();
The results are Strings
which are actually just large numbers stored as String
since Realm does not support BigInteger
. What I need to do is output those numbers in numerical order.
Cannot use Collections(sort)
since min sdk is 16. No luck using StreamSupport lib either. I am currently trying to convert RealmResults
to a simple array of strings so I can do the sorting on that but I'm having trouble. The following results in a cast error, but even before changes would result in ldrStrings.get(0)
outputting something like proxy[((75000))]
which is the real number but surrounded by that text:
RealmResults<Leaderboard> leaderboardList = realm.where(Leaderboard.class).distinct("score").findAll(); Leaderboard[] leaderboardArray = (Leaderboard[]) leaderboardList.toArray(); List<String> ldrStrings = new ArrayList(leaderboardArray.length); for (Object object : leaderboardArray) { ldrStrings.add(object != null ? object.toString() : null); } Collections.sort(ldrStrings, new Comparator<String>() { @Override public int compare(String o1, String o2) { BigInteger bi1 = new BigInteger(o1); BigInteger bi2 = new BigInteger(o2); return bi1.compareTo(bi2); } });
For posterity: this problem is way over-thought. Here is the solution which takes a RealmResults list, iterates the results into a string array, converts string to BigInteger and sorts them from largest to smallest.
RealmResults<Leaderboard> leaderboardList = realm.where(Leaderboard.class).distinct("score").findAll();
List<String> strings = new ArrayList<>();
for(int i = 0; i < leaderboardList.size(); i++) {
strings.add(leaderboardList.get(i).getScore());
}
Collections.sort(strings, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
BigInteger bi1 = new BigInteger(o1);
BigInteger bi2 = new BigInteger(o2);
return bi2.compareTo(bi1);
}
});