在Java中使用反应式扩展对对象集合进行排序?

在Java中使用反应式扩展对对象集合进行排序?,java,reactive-programming,rx-java,rx-android,Java,Reactive Programming,Rx Java,Rx Android,如何使用rxjava根据对象的一个或多个字段对对象集合进行排序 public class Car { public String model; public int numberOfWheels; public String color; public int yearOfProduction; } List<Car> cars = new ArrayList<>(); cars.add(...); cars.add(...); Obser

如何使用rxjava根据对象的一个或多个字段对对象集合进行排序

public class Car {

   public String model;
   public int numberOfWheels;
   public String color;
   public int yearOfProduction;

}

List<Car> cars = new ArrayList<>();
cars.add(...);
cars.add(...);

Observable<List<Car>> getCars() { Observable.just(cars) };

Observable<List<Car>> getCarsSortedByModel() { ??? };

Observable<List<Car>> getCarsSortedByColor() { ??? };

Observable<List<Car>> getCarsSortedByModelAndColor() { ??? };

公车{
公共字符串模型;
公共整数车轮;
公共字符串颜色;
公共生产年;
}
List cars=new ArrayList();
汽车。添加(…);
汽车。添加(…);
可观测的getCars(){Observable.just(cars)};
可观察的getCarsSortedByModel(){???};
可观察的GetCarsortedByColor(){???};
可观察的GetCarsortedByModelandColor(){???};

可观察。toSortedList
将返回
可观察

公车{
公共字符串模型;
公共整数车轮;
公共字符串颜色;
公共生产年;
公共静态void main(字符串[]args){
汽车()
.toSortedList(Car::compareModel)
.subscribe(System.out::println);
汽车()
.toSortedList(汽车::compareYear)
.subscribe(System.out::println);
}
专用静态整数比较模型(Car car1、Car car2){
返回car1.model.compareTo(car2.model);
}
专用静态整数比较器(Car car1、Car car2){
返回整数.valueOf(car1.生产年份)
.与(car2.生产年份)相比;
}
私人静态可观察车(){
Car car1=新车();
car1.model=“罗宾”;
car1.color=“红色”;
car1.numberOfWheels=3;
car1.生产年份=1972年;
Car car2=新车();
car2.model=“花冠”;
car2.color=“白色”;
car2.numberOfWheels=4;
car2.生产年份=1992年;
可观察到的返回。正义(car1,car2);
}
公共字符串toString(){
收益模型;
}
}

可观察。toSortedList
将返回
可观察

公车{
公共字符串模型;
公共整数车轮;
公共字符串颜色;
公共生产年;
公共静态void main(字符串[]args){
汽车()
.toSortedList(Car::compareModel)
.subscribe(System.out::println);
汽车()
.toSortedList(汽车::compareYear)
.subscribe(System.out::println);
}
专用静态整数比较模型(Car car1、Car car2){
返回car1.model.compareTo(car2.model);
}
专用静态整数比较器(Car car1、Car car2){
返回整数.valueOf(car1.生产年份)
.与(car2.生产年份)相比;
}
私人静态可观察车(){
Car car1=新车();
car1.model=“罗宾”;
car1.color=“红色”;
car1.numberOfWheels=3;
car1.生产年份=1972年;
Car car2=新车();
car2.model=“花冠”;
car2.color=“白色”;
car2.numberOfWheels=4;
car2.生产年份=1992年;
可观察到的返回。正义(car1,car2);
}
公共字符串toString(){
收益模型;
}
}

可观察。toSortedList
将返回
可观察

公车{
公共字符串模型;
公共整数车轮;
公共字符串颜色;
公共生产年;
公共静态void main(字符串[]args){
汽车()
.toSortedList(Car::compareModel)
.subscribe(System.out::println);
汽车()
.toSortedList(汽车::compareYear)
.subscribe(System.out::println);
}
专用静态整数比较模型(Car car1、Car car2){
返回car1.model.compareTo(car2.model);
}
专用静态整数比较器(Car car1、Car car2){
返回整数.valueOf(car1.生产年份)
.与(car2.生产年份)相比;
}
私人静态可观察车(){
Car car1=新车();
car1.model=“罗宾”;
car1.color=“红色”;
car1.numberOfWheels=3;
car1.生产年份=1972年;
Car car2=新车();
car2.model=“花冠”;
car2.color=“白色”;
car2.numberOfWheels=4;
car2.生产年份=1992年;
可观察到的返回。正义(car1,car2);
}
公共字符串toString(){
收益模型;
}
}

可观察。toSortedList
将返回
可观察

公车{
公共字符串模型;
公共整数车轮;
公共字符串颜色;
公共生产年;
公共静态void main(字符串[]args){
汽车()
.toSortedList(Car::compareModel)
.subscribe(System.out::println);
汽车()
.toSortedList(汽车::compareYear)
.subscribe(System.out::println);
}
专用静态整数比较模型(Car car1、Car car2){
返回car1.model.compareTo(car2.model);
}
专用静态整数比较器(Car car1、Car car2){
返回整数.valueOf(car1.生产年份)
.与(car2.生产年份)相比;
}
私人静态可观察车(){
Car car1=新车();
car1.model=“罗宾”;
car1.color=“红色”;
car1.numberOfWheels=3;
car1.生产年份=1972年;
Car car2=新车();
car2.model=“花冠”;
car2.color=“白色”;
car2.numberOfWheels=4;
car2.生产年份=1992年;
可观察到的返回。正义(car1,car2);
}
公共字符串toString(){
收益模型;
}
}
public class Car {
  public String model;
  public int numberOfWheels;
  public String color;
  public int yearOfProduction;

  public static void main(String[] args) {
    cars()
       .toSortedList(Car::compareModel)
       .subscribe(System.out::println) ;

    cars()
       .toSortedList(Car::compareYear)
       .subscribe(System.out::println) ;

  }

  private static Integer compareModel(Car car1, Car car2) {
    return car1.model.compareTo(car2.model);
  }

  private static Integer compareYear(Car car1, Car car2) {
    return Integer.valueOf(car1.yearOfProduction)
            .compareTo(car2.yearOfProduction);
  }

  private static Observable<Car> cars(){
    Car car1 = new Car();
    car1.model = "robin";
    car1.color = "red";
    car1.numberOfWheels = 3;
    car1.yearOfProduction = 1972;
    Car car2 = new Car();
    car2.model = "corolla";
    car2.color = "white";
    car2.numberOfWheels = 4;
    car2.yearOfProduction = 1992;
    return Observable.just(car1, car2);
  }

  public String toString(){
    return model;
  }
}