简体   繁体   English

使用比较器对列表进行排序不起作用

[英]Sorting on list using comparator is not working

I want to sort list of product data on the basis of axis type count.I am not able to find the issue in below code . 我想根据轴类型计数对产品数据列表进行排序。我无法在以下代码中找到问题。 How can i sort this result? 我该如何对结果进行排序?

private void sortResults(List<ProductData> results) {
        Collections.sort(results, new Comparator<ProductData>() {
            @Override
            public int compare(ProductData product1, ProductData product2) {
                return product1.getAxisTypeCount().compareTo(product2.getAxisTypeCount());
            }
        });}


ProductSearchPageData<SearchStateData, ProductData> searchPageData = null;
            searchPageData = encodeSearchPageData(productSearchFacade.textSearch(searchState, pageableData));
sortResults(searchPageData.getResults());

The expected result: Makeup 37 , Watches 7 , jewelry 6 , skincare 6, Fragrance 4 预期结果: Makeup 37 , Watches 7 , jewelry 6 , skincare 6, Fragrance 4

The actual result: Makeup 37 , Fragrance 4 , jewelry 6 , skincare 6,Watches 7 实际结果: Makeup 37 , Fragrance 4 , jewelry 6 , skincare 6,Watches 7

private void sortResults(List<ProductData> results) {
    Collections.sort(results, new Comparator<ProductData>() {
        @Override
        public int compare(ProductData product1, ProductData product2) {
            return product1.getAxisTypeCount().compareTo(product2.getAxisTypeCount());
        }
    });}

product1 would take second value from the list, whereas product2 will take first value from the list. product1从列表中获取第二个值,而product2从列表中获取第一个值。 For instance: 例如:

Your list is [Watches , jewelry, Fragrance, .........] On calling compare function product1 = jewelry and product2 = Watches 您的清单为[Watches , jewelry, Fragrance, .........]调用比较功能时product1 = jewelryproduct2 = Watches

I am assuming getAxisTypeCount() is giving integer value, if your object is "Makeup 37", getAxisTypeCount() would return 37 我假设getAxisTypeCount()给出整数值,如果您的对象是“ Makeup 37”,则getAxisTypeCount()将返回37

This line => 这行=>

return product1.getAxisTypeCount().compareTo(product2.getAxisTypeCount());

would result: 将导致:

Fragrance   4
jewelry   6
skincare   6
Watches   7
Makeup   37

not as you mentioned in the actual result Makeup 37 , Fragrance 4 , jewelry 6 , skincare 6, Watches 7 不像您在实际结果中提到的Makeup 37 , Fragrance 4 , jewelry 6 , skincare 6, Watches 7

If you want your compare method should result output as you mentioned in expected result: Makeup 37 , Watches 7 , jewelry 6 , skincare 6, Fragrance 4 如果您希望比较方法应按预期结果输出结果: Makeup 37 , Watches 7 , jewelry 6 , skincare 6, Fragrance 4

This line return product2.getAxisTypeCount().compareTo(product1.getAxisTypeCount()) will solve you problem. 此行return product2.getAxisTypeCount().compareTo(product1.getAxisTypeCount())将解决您的问题。

private void sortResults(List<ProductData> results) {
    Collections.sort(results, new Comparator<ProductData>() {
        @Override
        public int compare(ProductData product1, ProductData product2) {
            return product2.getAxisTypeCount().compareTo(product1.getAxisTypeCount());
        }
    });}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM