androidjava中的排序列表

jyztefdp  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(343)

我一直在尝试在android中对一个方法列表进行排序,但到目前为止还无法完成。下面是实现

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

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

我正在尝试按标题对该方法返回的列表按以下方式进行排序,但它没有按要求的顺序进行排序。

List<Items> a = new ArrayList(Collections.singletonList(getItems(getActivity())));

        Collections.sort(a, new Comparator<Items>() {
            @Override
            public int compare(Items o1, Items o2) {
                if (o1 == null) return -1;
                else if (o2 == null) return 1;
                int cmp = String.CASE_INSENSITIVE_ORDER.compare(o1.getTitle(), o2.getTitle());
                if (cmp != 0) return cmp;
                return o1.getTitle().toLowerCase().compareTo(o2.getTitle().toLowerCase());
            }
        });
kgqe7b3p

kgqe7b3p1#

比较预期并不明确。
下面的代码将对char与char进行比较,直到它们具有相同的长度,如果长度过大,则返回other。
请注意:该代码忽略该情况。如我所见,你需要忽略这个案例。

public static void main(String[] args) {

    TestCheck t = new TestCheck();
    List<Item> itemList = t.generateList();
    Comparator<Item> itemComparator = (o1, o2) -> {
        if(o1 == null) return -1;
        else if(o2 == null) return 1;
        char[] o1Item = o1.getTitle().toLowerCase().toCharArray();
        char[] o2Item = o2.getTitle().toLowerCase().toCharArray();
        int minLen = Math.min(o1Item.length, o2Item.length);
        for(int value = 0; value < minLen; value++) {
            int diff = o1Item[value] - o2Item[value];
            if(diff != 0) return diff;
        }
        if(o1Item.length == minLen) return 1;
        else return 0;
    };
    itemList.sort(itemComparator);
    itemList.forEach(System.out::println);
}

private List<Item> generateList() {
    List<Item> itemList = new LinkedList<>();
    IntStream.range(1, 10).forEach(value -> {
        itemList.add(new Item(value + "", "path", "title" + value));
    });
    itemList.add(new Item("13", "path", "winWIN"));
    itemList.add(new Item("14", "path", "XNInwd"));
    itemList.add(new Item("15", "path", "LWNDN"));
    itemList.add(new Item("16", "path", "ziubnd"));
    itemList.add(new Item("48", "path", "32bjsjdbf"));
    itemList.add(new Item("48", "path", "12bjsjdbf"));

    return itemList;
}

输出:
项{id='48',path='path',项='12bjsjdbf'}
项{id='48',path='path',项='32bjsjdbf'}
项目{id='15',path='path',item='lwndn'}
项{id='1',path='path',项='title1'}
项{id='2',path='path',项='title2'}
项{id='3',path='path',项='title3'}
项{id='4',path='path',项='title4'}
项{id='5',path='path',项='title5'}
项{id='6',path='path',项='title6'}
项{id='7',path='path',项='title7'}
项{id='8',path='path',项='title8'}
项{id='9',path='path',项='title9'}
项{id='13',path='path',项='winwin'}
项目{id='14',path='path',item='xninwd'}
项目{id='16',path='path',item='ziubnd'}

相关问题