【问题标题】:How to sort a List by checking a value inside inner list in Java?如何通过检查Java内部列表中的值来对列表进行排序?
【发布时间】:2021-11-25 13:27:41
【问题描述】:

我有一个像下面这样的课程,

public class Inventory {

   private String name;
   private List<Sample> samples;
}

public class Sample {

   private int count;
   private String date;
}

List<Inventory> inventories;

我已按日期对samples 进行了排序。没关系。但是我需要通过 Sample 类中的date 字段对inventories 列表进行排序。 samples 已经排序。所以我需要按样本日期 DESC 对库存进行排序。例如,

inventories 列表中,如果它有 3 个数据,

Inventory 1
name = 1
samples has dates of = List of (2021-07-02, 2021-07-03)

Inventory 2
name = 2
samples has dates of = List of (2021-07-02, 2021-09-03, 2021-10-03)

Inventory 3
name = 3
samples has dates of = List of (2021-08-02, 2021-09-03)

所以它应该按日期 DESC 排序,并且在那个 inventories 列表中,它应该有以下方式的数据,

Inventory 1
Inventory 3
Inventory 2

【问题讨论】:

  • 应如何根据采样日期对库存进行排序?您想为每个库存使用最早还是最晚的采样日期?

标签: java java-stream


【解决方案1】:

您希望根据嵌套集合中的值对集合进行排序。 我们该怎么做?

您需要实现一个自定义比较器来执行此操作...

static class InventoryComparator implements Comparator<Inventory> {
     public int compare(Inventory i1, Inventory i2) {
         // compare them here like this 
         // a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
         // this can be done by checking the nested lists
     }
}

然后你可以像这样对你的收藏进行排序......

Collections.sort(inventoryList, new InventoryComparator());

【讨论】:

  • 非常感谢您的回答和您的宝贵时间。
【解决方案2】:

这可以通过Comparable 接口实现。

我假设Sample 对象按日期排序,Inventory 对象通过比较samples 中的最后一个(Sample 与最新日期)Sample 进行排序,但您可以实现自己的自定义比较 Samples 中覆盖的 compareTo() 方法中的逻辑。

  public class Inventory implements Comparable<Inventory> {

    private String name;
    private List<Sample> samples;

    public List<Sample> getSamples() {
      return samples;
    }

    public Inventory(String name, List<Sample> samples) {
      this.name = name;
      this.samples = samples;
    }

    @Override
    public int compareTo(Inventory inventory) {
      Optional<Sample> thisOldestSample = this.getSamples().stream().sorted().reduce((s1, s2) -> s2);
      Optional<Sample> thatOldestSample = inventory.getSamples().stream().sorted().reduce((s1, s2) -> s2);

      if (thisOldestSample.isPresent() && thatOldestSample.isPresent()) {
        return thisOldestSample.get().compareTo(thatOldestSample.get());
      } else {
        return 0;
      }
    }
  }

  public class Sample implements Comparable<Sample> {

    private int count;
    private String date;

    public String getDate() {
      return date;
    }

    public Sample(int count, String date) {
      this.count = count;
      this.date = date;
    }

    @Override
    public int compareTo(Sample sample) {
      return LocalDate.parse(sample.getDate()).isBefore(LocalDate.parse(this.getDate())) ? 1 : -1;
    }
  }

  @Test
  void shouldSortInventoriesBasedOnSampleDate() {
    Inventory one = new Inventory("1", List.of(new Sample(0, "2021-07-02"), new Sample(1, "2021-07-03")));
    Inventory two = new Inventory("2", List.of(new Sample(0, "2021-07-02"), new Sample(1, "2021-09-03"), new Sample(2, "2021-10-03")));
    Inventory three = new Inventory("3", List.of(new Sample(0, "2021-08-02"), new Sample(1, "2021-09-03")));

    List<Inventory> unsorted  = List.of(one, two, three);
    List<Inventory> sorted = unsorted.stream().sorted().collect(Collectors.toList());

    assertEquals(one, sorted.get(0));
    assertEquals(three, sorted.get(1));
    assertEquals(two, sorted.get(2));
  }

【讨论】:

  • 非常感谢您的回答和您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
  • 2021-02-05
  • 2012-01-15
  • 2015-02-18
  • 2020-12-23
相关资源
最近更新 更多