Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何对不同的日期进行排序?_Java_Spring - Fatal编程技术网

Java 如何对不同的日期进行排序?

Java 如何对不同的日期进行排序?,java,spring,Java,Spring,我想按不同的日期(creationDate、displayDate和requestDate)对Foo类中的产品列表进行排序,然后返回产品列表。 你有一个有效的建议吗 public class Car extend Product { String name; Date creationDate; Product(String name, Date creationDate){ this.name = name; this.creationDate= cre

我想按不同的日期(creationDate、displayDate和requestDate)对Foo类中的产品列表进行排序,然后返回产品列表。 你有一个有效的建议吗

public class Car extend Product {

   String name;

   Date creationDate;

   Product(String name, Date creationDate){
    this.name = name;
    this.creationDate= creationDate;
   }
}

public class Bicycle extend Product {
   String name;

   Date displayDate;

   Bicycle(String name, Date displayDate){
    this.name = name;
    displayDate= displayDate;
   }


}

public class Motorcycle extend Product {
   String name;

   Date requestDate;

   Motorcycle (String name, Date requestDate;{
    this.name = name;
    requestDate=requestDate;;
   }


}

public类产品配置(){
List cars=new ArrayList();
cars.add(新车(“吉普”,新日期());
列出自行车=新建ArrayList();
自行车。添加(新自行车(“mountainbike”,新日期());
列出摩托车=新ArrayList();
摩托车.添加(新摩托车(“摩托车”,新日期());
//接二连三
}
我想按不同的日期(creationDate、displayDate和requestDate)对Foo类中的产品列表进行排序,然后返回产品列表

public class Foo {


  public List<Product> readData() {  
    ProductConfiguration productConfiguration = new ProductConfiguration();

    List<Product> products = new ArrayList<>();

    products.add(productConfiguration.getCars());
    products.add(productConfiguration.getBicycles());
    products.add(productConfiguration.getMotorcycles());


   public List<Product> sortProductByDate(){
      // I want to sort the list products by date (creationDate, displayDate 
         and requestDate)
         and return the Product List products

   }

  }
}
公共类Foo{
公共列表readData(){
ProductConfiguration ProductConfiguration=新产品配置();
列表产品=新的ArrayList();
products.add(productConfiguration.getCars());
products.add(productConfiguration.getBicycles());
products.add(productConfiguration.getMotorkes());
公共列表sortProductByDate(){
//我想按日期(creationDate、displayDate)对列表产品进行排序
(日期)
并返回产品列表中的产品
}
}
}

正如@Seelenvirtuose所建议的,我还建议您在产品类中添加一个日期字段:

public class Product // alternative: create an interface which your classes can implement
{
    private String name;
    private Date date;

    public Product(String name, Date date)
    {
        this.name = name;
        this.date = date;
    }

    // getter / setter
}
然后您可以执行以下操作:

    Product bike = new Product("Bike", new Date()); // just for example. You can create a specialized class Bike with additional fields. This is just for demonstration purposes
    Product car = new Product("Car", new Date()); // see above

    List<Product> productList = new ArrayList<>();
    productList.add(bike);
    productList.add(car);

    productList.sort(new Comparator<Product>()
    {
        @Override
        public int compare(Product o1, Product o2)
        {
            return o1.date.compareTo(o2.date);
        }
    });

   // after this your list is sorted by date
Product bike=new Product(“bike”,new Date());//例如,您可以创建一个带有附加字段的专用类bike。这只是为了演示
Product car=新产品(“car”,new Date());//见上文
List productList=new ArrayList();
productList.add(自行车);
productList.add(car);
productList.sort(新的Comparator()
{
@凌驾
公共整数比较(产品o1、产品o2)
{
返回o1.日期比较(o2.日期);
}
});
//在此之后,您的列表将按日期排序

设计没有多大意义。不妨在
产品
级别上定义一个
orderDate
属性。如果每个产品都有一个订单日期,那么为什么不干脆在
产品
类中添加一个
orderDate
字段呢?这样你就可以简单地对列表进行排序。@RobbyCornelissen:日期有不同的meanings。我已经更新了我的帖子。整个示例感觉很做作。你想对一个属性列表进行排序,但该列表中三分之二的对象不具有该属性。你显然在试图实现某些目标,但你的问题并不清楚这是什么。
    Product bike = new Product("Bike", new Date()); // just for example. You can create a specialized class Bike with additional fields. This is just for demonstration purposes
    Product car = new Product("Car", new Date()); // see above

    List<Product> productList = new ArrayList<>();
    productList.add(bike);
    productList.add(car);

    productList.sort(new Comparator<Product>()
    {
        @Override
        public int compare(Product o1, Product o2)
        {
            return o1.date.compareTo(o2.date);
        }
    });

   // after this your list is sorted by date