【问题标题】:How to sort data by ascending order in Firebase Database?如何在 Firebase 数据库中按升序对数据进行排序?
【发布时间】:2025-11-21 23:25:01
【问题描述】:

我正在使用 order by child 将数据 spisplay 到 listView 并且数据按降序显示,如何在 Android 客户端中将顺序设置为升序?

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();


                }

我想使用 Arraylist 集合对从数据库中获取的数据进行排序。

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);

我不知道如何实现。我怎样才能使用这个集合?如何使用列表来提供 feedItems?

【问题讨论】:

    标签: android firebase arraylist collections firebase-realtime-database


    【解决方案1】:

    根据您的解释,您只想颠倒列表的顺序。 这可以使用比较器轻松完成。

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

    此代码应将您的列表倒序排列。 希望我有所帮助,否则,请提供有关逆序阶段的更多信息。

    【讨论】:

    • 我的 feedItems 是书籍列表List&lt;Book&gt; feedItems = new ArrayList&lt;Book&gt;(); 我的另一个列表是字符串列表List&lt;String&gt; list = new ArrayList&lt;String&gt;(); 如何使用列表来提供 feedItems ?
    • @Amal 您可以简单地实例化 Book 类 item = new Book(); 的新构造函数,将 list 中的项目添加到构造函数中。最后,将该项目添加到您的 feedItems ArrayList feedItems.add(item); 中。如果不符合您的预期,请向我提供有关您的问题的更多信息。
    • 我有这个错误:java.lang.ClassCastException: com.mydreambook.model.Book cannot be cast to java.lang.Comparable at java.util.Collections$ReverseComparator.compare(Collections.java:192)Collections.sort(feedItems,compare);
    • 我必须在我的类模型中添加implements Comparable&lt;Book&gt;
    • @Amal 很高兴您解决了这个问题。如果您需要任何其他帮助,请在此处回复。
    【解决方案2】:

    我想按升序对数据进行排序,解决方案就是使用集合来反转我拥有的顺序:

    Collections.reverse(feedItems);
    

    【讨论】: