0

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!

zngb
  • 601
  • 1
  • 7
  • 24
  • 1
    Try Collator if it's available http://www.javapractices.com/topic/TopicAction.do?Id=207 – LMC May 29 '18 at 21:31

1 Answers1

1

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());
DavidW
  • 1,413
  • 10
  • 17
  • `stream()` , `sorted()` ,`collect()` and `Collectors.toList()` all require api 24 (min is 16) unfortuntely – zngb May 29 '18 at 22:44
  • @zngb Ah, sorry. You didn't specify... I suppose you could just copy them into a list manually and sort them? – DavidW May 29 '18 at 22:52
  • I tried that and got those incompatibility issues.. do you have an example of what I could try? Thank you – zngb May 29 '18 at 23:47
  • Use static method `Collections.sort();` to apply the comparator if you cannot use streams – Maelig Aug 02 '18 at 12:47