简体   繁体   English

在Java中使用集合排序列表的问题

[英]Issue with Sorting List using collections in java

I have a list with Order Entries. 我有一个包含订单条目的列表。 Each order entry has a base price. 每个订单条目都有一个底价。 I want to sort this list on entry.basePrice descending (enty.basePrice == 0 entries should be at the bottom). 我想在entry.basePrice降序排序此列表(enty.basePrice == 0条目应在底部)。

In this list, either no entry will have 0 price or one entry will have. 在此列表中,没有条目将具有0价格或一个条目将具有0价格。 I am doing like this .. 我是这样

final Collection<AbstractOrderEntryModel> orderEntry = Collections2.filter(source.getEntries(),
            new Predicate<AbstractOrderEntryModel>()
            {
                @Override
                public boolean apply(final AbstractOrderEntryModel arg)
                {
                    return arg.getBasePrice().doubleValue() == 0 ? true : false;
                }
            });

Here I m filtering my entry which having baseprice = 0.0 Now How I will remove and add this item (orderEntry.iterator().next()) at last in OrderEntry List? 在这里,我正在过滤我的输入,该输入的baseprice = 0.0现在如何在OrderEntry列表中最后删除并添加此项(orderEntry.iterator().next())

If its not a recommended solution, and it can be possible through Collections.sort also then please give me solution. 如果它不是推荐的解决方案,也可以通过Collections.sort来解决,请给我解决方案。

As far as I understand, you want to put an item matching a predicate at the end of the list. 据我了解,您希望将与谓词匹配的项目放在列表的末尾。 This can be done straightforward: 这可以很简单地完成:

List<AbstractOrderEntryModel> list=source.getEntries()
final Collection<AbstractOrderEntryModel> orderEntry = Collections2.filter(list,
  new Predicate<AbstractOrderEntryModel>()
  {
    public boolean apply(final AbstractOrderEntryModel arg)
    {
        return arg.getBasePrice().doubleValue() == 0;
    }
  });
if(!orderEntry.isEmpty()) {
  assert orderEntry.size()==1; // so you said in your question
  AbstractOrderEntryModel e=orderEntry.iterator().next();
  list.remove(e);
  list.add(e);
}

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

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