Amal
Amal

Reputation: 299

How to sort data by ascending order in Firebase Database?

I'm using order by child to spisplay data into listView and the data is displayed by descending order, How can I set the order to ascending order in Android client Side?

ref.orderByChild("date_creation").limitToFirst(10).addValueEventListener(new ValueEventListener() {

//date_creation is the date when user add Book "dd-MM-yyyy_HH:mm:ss" 
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {


                    for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {

                        Book valueBook = dataSnap.getValue(Book.class);
                        String titreLivreToDisplay = valueBook.getNom_livre();
                        String descLivreToDisplay = valueBook.getDesc_livre();
                        String prixLivreToDisplay = valueBook.getPrix_livre();
                        String timeToDisplay = valueBook.getDate_creation();
                        String filePathToDiplay = valueBook.getChemin_image();
                        String villeToDisplay = valueBook.getVille_livre();
                        String typeAnnToDisplat = valueBook.getType_annonce_selected();

                        item = new Book();

                        item.setNom_livre(titreLivreToDisplay);
                        item.setDesc_livre(descLivreToDisplay);
                        item.setPrix_livre(prixLivreToDisplay);
                        item.setDate_creation(timeToDisplay);
                        item.setChemin_image(filePathToDiplay);
                        item.setVille_livre(villeToDisplay);
                        item.setType_annonce_selected(typeAnnToDisplat);


                        feedItems.add(item);


                    }


                    listAdapter = new FeedListAdapter(AccueilActivity.this, feedItems);

                    listView.setAdapter(listAdapter);
                    listAdapter.notifyDataSetChanged();


                }

I want use Arraylist collections to sort data that I get From Database.

List<String> list = new ArrayList<String>();
                    for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {

                        Book valueBook = dataSnap.getValue(Book.class);

                        String titreLivreToDisplay = valueBook.getNom_livre();
                        String descLivreToDisplay = valueBook.getDesc_livre();
                        String prixLivreToDisplay = valueBook.getPrix_livre();
                        String timeToDisplay = valueBook.getDate_creation();
                        String filePathToDiplay = valueBook.getChemin_image();
                        String villeToDisplay = valueBook.getVille_livre();
                        String typeAnnToDisplat = valueBook.getType_annonce_selected();

                        list.add(titreLivreToDisplay);
                        list.add( descLivreToDisplay);
                        list.add(prixLivreToDisplay);
                        list.add(timeToDisplay);
                        list.add(filePathToDiplay);
                        list.add( villeToDisplay);
                        list.add(typeAnnToDisplat);
Collections.sort(list);

I don't know how to implement that. How can I use this collections? How to use list to feed feedItems?

Upvotes: 0

Views: 3832

Answers (2)

Amal
Amal

Reputation: 299

I want sorted data by ascending order , the solution was just use collections to reverse order that I had:

Collections.reverse(feedItems);

Upvotes: 0

Imad
Imad

Reputation: 84

From what you explained, you only want to reverse the order of your list. This can be easily done using a comparator.

// Defining the comparator
Comparator compare = Collections.reverseOrder();

// Sorting the list taking into account the comparator
Collections.sort(list, compare);

This code should reverse order your list. Hope I have helped, otherwise, provide more information about the reverse order stage.

Upvotes: 2

Related Questions