-1

Is it possible to sort the current output of the code below in descending order without using comparators?

I've tried using one but I can't seem to make it work as it always gives an error.

I've tried looking at these two links as I know the answers might be in there: Sort Map<String, Long> by value reversed , Sort a Map<Key, Value> by values
But since I'm quite new to java I don't really get what I need to do in order to get it working.

myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot , String s) {
        Map<String, Object> userInfo = (Map<String, Object>)dataSnapshot.getValue();
        String username = (String) userInfo.get("username");

        Map<String, Long> steps = (Map<String, Long>) userInfo.get("steps");

        long existingSteps = 0;
        for (Map.Entry<String, Long> step : steps.entrySet()) {
            existingSteps += Long.valueOf(step.getValue());
        }

        arrayList.add(new String( username + "  -  " + "steps:  " + existingSteps));

        arrayAdapter = new ArrayAdapter<>(Leaderboard.this , 
        android.R.layout.simple_list_item_1 , arrayList);

        listView.setAdapter(arrayAdapter);
}

The current output of this code is:

John Doe I - Steps: 79
John Doe II - Steps: 111
John Doe III - Steps: 0
John Doe IV - Steps: 88
John Doe V - Steps: 12
John Doe VI - Steps: 0

preferred this code to run the following output:

John Doe II - Steps: 111
John Doe IV - Steps: 88
John Doe I - Steps: 79
John Doe V - Steps: 12
John Doe III - Steps: 0
John Doe VI - Steps: 0

Is it possible for someone to explain to me how I would need to use those said links to get it working for me so I could get the output as listed above ?

Molinho
  • 31
  • 1
  • 6
  • _"Is it possible to sort the current output of the code below in descending order without using comparators?"_ Maybe. But wouldn't a Comparator or a class implementing Comparable be the "right way"? _"I've tried using one but I can't seem to make it work as it always gives an error."_ Maybe you could show us the Comparator code and the error message. (You can still edit the question.) That would be more suitable for Stack Overflow than "Can somebody explain to me how...?" – Markus Kauppinen May 28 '19 at 13:38
  • You can sort it in client side.Take a look at [this answer.](https://stackoverflow.com/a/8491003/8516127) That's what you need. If you want to sort values at serverside; you have to use Firebase Queries. – furkanbzkurt May 28 '19 at 13:38
  • the values in my case are taken from map entries with a `Key, value`, is it also possible to use that answer to sort my arraylist when I don't have manually added values like his stated "abc" string ? @PinkRabbits – Molinho May 28 '19 at 14:10

1 Answers1

1

The Firebase Realtime Database can return child nodes ordered by the value of a property of each child node. But since you determine existingSteps by aggregating values from the database, there is no way to get the node from the database in this order, unless you also store the aggregated value in the database.

In your current data format, you will need to re-order the items in your array list client-side after aggregating the steps for all users.


If you were to modify the data format to include the aggregated existingSteps value, keep in mind that Firebase can only return results in ascending order. That means that to show them in reverse order, you'll either have to reverse the array list client-side, or store an additional property in the database that has the inverted value of existingSteps so that ordering on that gives the reversed results. For more on this, see firebase -> date order reverse

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • So is it impossible for me to use the methods that are explained in here: https://stackoverflow.com/questions/30083161/sort-mapstring-long-by-value-reversed ? – Molinho May 28 '19 at 14:01
  • That uses a `SortedMap`, which keeps the keys sorted. But you're using an `ArrayList`, which keeps the items in the order in which you add them. – Frank van Puffelen May 28 '19 at 14:16
  • so basically the answer I was given here, https://stackoverflow.com/questions/56326546/how-to-sort-the-key-value-of-a-map-entry-in-descending-order, is the only option I have ? – Molinho May 28 '19 at 14:24