dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Apr 23, 2020

Chaining Ordering in 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;
      }
});
Octavia Anghel
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date