zngb
zngb

Reputation: 633

Sort RealmResults strings numerically?

I need to sort this list of String which are actually large numbers (had to use that since BigInteger is not supported with Realm)

RealmResults<Leaderboard> leaderboardList = realm.where(Leaderboard.class).distinct("score").findAll().sort("score",Sort.DESCENDING);

The results are 5 Strings with the following numbers:
75,000
74,990
6,079,990
5,006,079,990
1,079,990

which display in that order when sorted by Sort.DESCENDING

I need to actually sort them correctly, and cannot get any solution with Collection working with the RealmResults list. Also having trouble using the toArray() method of RealmResults since in all cases there is some problem with differing types that I do not understand.

Would appreciate any help, thanks!

Upvotes: 0

Views: 263

Answers (1)

DavidW
DavidW

Reputation: 1421

RealmResults implements java.util.Collection so can't you just write

Comparator<Leaderboard> descendingScore = (l1, l2) ->
       (new BigDecimal(l2.getScore()).compareTo(new BigDecimal(l1.getScore()));
List<Leaderboard> leaderboardList = realm.where(Leaderboard.class)
    .distinctValues("score")
    .findAll()
    .stream()
    .sorted(descendingScore)
    .collect(Collectors.toList());

Upvotes: 1

Related Questions