Custom sorting of list of objects using Java Stream

Keval Padsumbiya
2 min readFeb 10, 2023

--

Java

Sometimes you need to sort list of objects such that few elements are sorted in ascending order and few elements are sorted in descending order.

Let’s say you have below Java POJO class:

package com.custom.sorting.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class Seller {
String sellerName;
Double distanceFromMyLocation;
String sellerState;
Double sellerRating;
}

Now, say you have list of Seller objects and want to sort as mentioned below such that:

  1. list will be sorted in ascending order of distanceFromMyLocation
  2. list will be sorted in descending order of sellerRating
  3. list will be sorted in ascending order of sellerName
  4. list will be sorted in ascending order of sellerState

To do as above we can use Comparator class of Java.

We can write separate comparators for each attributes to be used in sorting.

Java code:

package com.custom.sorting.service;

import com.custom.sorting.model.Seller;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class CustomSorting {

public static void main(String[] args) {
List<Seller> sellerList = getSampleListOfSellers();

//seller name comparator
Comparator<Seller> sellerNameComparator = Comparator.comparing(Seller::getSellerName);

//seller location distance comparator
Comparator<Seller> sellerLocationDistanceComparator = Comparator.comparing(Seller::getDistanceFromMyLocation);

//seller state comparator
Comparator<Seller> sellerStateComparator = Comparator.comparing(Seller::getSellerState);

//seller rating comparator
Comparator<Seller> sellerRatingComparator = Comparator.comparing(Seller::getSellerRating);

Comparator<Seller> finalComparator = sellerLocationDistanceComparator
.thenComparing(sellerRatingComparator.reversed())
.thenComparing(sellerNameComparator)
.thenComparing(sellerStateComparator);

List<Seller> sortedSellerList = sellerList.stream()
.sorted(finalComparator)
.collect(Collectors.toList());

sortedSellerList.forEach(seller -> System.out.println(seller.toString()));
}

public static List<Seller> getSampleListOfSellers() {
return Arrays.asList(
getSeller("Alice", 60.0, "Goa", 3.8),
getSeller("Bob", 12.0, "Delhi", 4.2),
getSeller("Alex", 12.0, "Kerala", 4.5),
getSeller("Roy", 56.7, "Maharashtra", 4.4),
getSeller("Mark", 125.0, "Gujarat", 4.0),
getSeller("Eric", 125.0, "Gujarat", 4.0)
);
}

public static Seller getSeller(String sellerName,
Double distanceFromMyLocation,
String sellerState,
Double sellerRating) {
return Seller.builder().sellerName(sellerName)
.distanceFromMyLocation(distanceFromMyLocation)
.sellerState(sellerState)
.sellerRating(sellerRating)
.build();
}

}

Output:

Seller(sellerName=Alex, distanceFromMyLocation=12.0, sellerState=Kerala, sellerRating=4.5)
Seller(sellerName=Bob, distanceFromMyLocation=12.0, sellerState=Delhi, sellerRating=4.2)
Seller(sellerName=Roy, distanceFromMyLocation=56.7, sellerState=Maharashtra, sellerRating=4.4)
Seller(sellerName=Alice, distanceFromMyLocation=60.0, sellerState=Goa, sellerRating=3.8)
Seller(sellerName=Eric, distanceFromMyLocation=125.0, sellerState=Gujarat, sellerRating=4.0)
Seller(sellerName=Mark, distanceFromMyLocation=125.0, sellerState=Gujarat, sellerRating=4.0)

Thank you for reading and please follow if you like the article.

Subscribe to stay tuned for upcoming articles.

--

--

Keval Padsumbiya
Keval Padsumbiya

Written by Keval Padsumbiya

Competitive Programmer and Software Engineer with experience in Java, Spring Boot, AWS, Jooq, SQL, Terraform etc

No responses yet