如何在android java中按字母顺序对arraylist排序

pb3s4cty  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(210)

我有一个返回arraylist的方法,我想按字母顺序对arraylist排序。我一直试图这样做,但它没有做任何事情。下面是实现

public List<Items> getItems(@NotNull Context context){
        List<Items> tempList = new ArrayList<>();
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String [] projection = {
                MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.TITLE,

        };
        Cursor cursor = context.getContentResolver().query(uri,projection,null,null,null);
        if (cursor != null){
            while (cursor.moveToNext()){
                String id = cursor.getString(0);
                String path = cursor.getString(1);
                String title = cursor.getString(2);

                Items items= new Items(id,path,title);

                tempList.add(items);
            }
            cursor.close();
        }
        return tempVideos;

这是提供arraylist的给定方法。我尝试按以下方式对其进行排序,但它只是按默认的插入顺序添加项。

Comparator<Items> comparator = new Comparator<Items>() {
                    @Override
                    public int compare(Items o1, Items o2) {

                    return o1.getTitle().toString().toLowerCase().
                        compareTo(o2.getTitle().toString().toLowerCase());
                    }
     };

    List<Items> myList = getItems(getActivity());
    Collections.sort(myList), comparator);

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题