Tan Kahseng
Tan Kahseng

Reputation: 71

Firebase sorting data in descending order. (only half sorted.)

I am trying to sort my listview into descending order by the value. I have tried using Collections.reverse(list); but I dont know why only half the list is sorted into descending. The bottom half is in ascending order.

The Following is my Code:

com.firebase.client.Query Queryref = mRef.orderByValue().limitToLast(20);
Queryref.addChildEventListener(new com.firebase.client.ChildEventListener() {
    @Override
    public void onChildAdded(com.firebase.client.DataSnapshot dataSnapshot, String s) {
        String value = dataSnapshot.getValue(String.class);
        String boothname = dataSnapshot.getKey();
        list.add(new Booth(boothname, value));
        adapter.notifyDataSetChanged();

        Collections.reverse(list);
    }

The Code above allow me to display the top 20 highest value in ascending. But I want it to be in descending and tried using Collections.reverse(); But the output I received show only the first 10 data in descending. Please help :(

Upvotes: 0

Views: 596

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598797

An alternative to Doug's approach is to insert each child at the start of the list, instead of adding to the end of the list as you do now.

For example, if your list variable is an List you can insert at the start with add(index, value):

list.add(0, new Booth(boothname, value));

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317457

You're reversing the list after each child individual child is added to it. That doesn't sound at all like it's going to be a sorted list in the end.

You probably don't want to use a ChildEventListener. Use a ValueEventListener to get a hold of all the children at once, then sort them when that's done.

Upvotes: 1

Related Questions