dcsimg
 

Submitted by: Octavia Anghel(octaviaanghel@gmail.com)

Thursday Apr 23rd 2020 by Octavia Anghel

Language: Java, Expertise: Intermediate: Learn how to sort an array by chaining ordering with Guava.

Guava allows us to sort an array by chaining ordering, as shown in the example below:

List cars = Arrays.asList(
  new Car("Dacia", 21000),
  new Car("Audi", 30000), 
  new Car("Fiat", null));
  
Ordering ordering = Ordering
  .natural()
  .nullsFirst()
  .onResultOf(new Function() {
      @Override
      public Comparable apply(Car car) {
          return car.price;
      }
});
Home
Mobile Site | Full Site