public class Leaderboard extends RealmObject {
String score;
}
RealmResults<Leaderboard> leaderboardList = realm.where(Leaderboard.class).distinct("score").findAll();
结果为Strings
,实际上只是存储为String
的大数字,因为Realm不支持BigInteger
。我需要做的是以数字顺序输出这些数字。
由于 min sdk为16 ,因此无法使用Collections(sort)
。使用StreamSupport lib也没有运气。我目前正在尝试将RealmResults
转换为简单的字符串数组,以便我可以对其进行排序,但我遇到了麻烦。以下结果会导致播放错误,但即使在更改之前,ldrStrings.get(0)
也会输出类似proxy[((75000))]
的内容,这是真实数字但被该文本包围:
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); } });
答案 0 :(得分:1)
对于后人:这个问题过于思考了。以下是获取RealmResults列表的解决方案,将结果迭代为字符串数组,将字符串转换为BigInteger并将它们从大到小排序。
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);
}
});