🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Sorting a list in ascending and descending order is a fundamental operation in Java, allowing for data organisation for further processing or presentation. Custom sorting criteria can be particularly useful when working with complex data types or specific sorting requirements. Java's Collections. sort() method, along with a custom Comparator, provides a flexible way to sort lists according to custom-defined rules. This blog post will illustrate how to sort a list of custom objects in Java first in ascending order and then in descending order.
2. Program Steps
1. Define a class to represent the objects in the list.
2. Create and populate a list with instances of the class.
3. Define two custom Comparators for ascending and descending order sorting.
4. Use the Collections.sort() method with the custom Comparator to sort the list in ascending order.
5. Repeat the sorting using the Comparator for descending order.
6. Display the sorted list after each sort to illustrate the order.
3. Code Program
import java.util.*;
// Step 1: Defining the class
class Fruit {
String name;
int quantity;
Fruit(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
@Override
public String toString() {
return name + ": " + quantity;
}
}
public class CustomSortListOrder {
public static void main(String[] args) {
// Step 2: Creating and populating the list
List<Fruit> fruits = new ArrayList<>();
fruits.add(new Fruit("Apple", 50));
fruits.add(new Fruit("Orange", 20));
fruits.add(new Fruit("Banana", 40));
// Step 3: Defining custom Comparators
Comparator<Fruit> sortByQuantityAscending = Comparator.comparingInt(f -> f.quantity);
Comparator<Fruit> sortByQuantityDescending = (f1, f2) -> f2.quantity - f1.quantity;
// Step 4: Sorting in ascending order
Collections.sort(fruits, sortByQuantityAscending);
System.out.println("Fruits sorted by quantity (ascending):");
fruits.forEach(System.out::println);
// Step 5: Sorting in descending order
Collections.sort(fruits, sortByQuantityDescending);
System.out.println("\nFruits sorted by quantity (descending):");
fruits.forEach(System.out::println);
}
}
Output:
Fruits sorted by quantity (ascending): Orange: 20 Banana: 40 Apple: 50 Fruits sorted by quantity (descending): Apple: 50 Banana: 40 Orange: 20
Explanation:
1. The Fruit class is defined with name and quantity fields, a constructor, and a toString method for display purposes.
2. A List<Fruit> named fruits is created and populated with Fruit objects.
3. Two Comparator<Fruit> objects are defined for sorting: sortByQuantityAscending uses the Comparator.comparingInt method for natural integer sorting (ascending), while sortByQuantityDescending implements a lambda expression to sort integers in reverse (descending).
4. The list is first sorted in ascending order using Collections.sort(fruits, sortByQuantityAscending), and the sorted list is printed to demonstrate the ascending order sorting.
5. The list is then sorted in descending order using Collections.sort(fruits, sortByQuantityDescending), and the sorted list is printed to show the descending order.
6. This approach illustrates how to use custom Comparators to sort a list of objects in Java according to ascending and descending order, highlighting the versatility and power of Java's Collections Framework.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment