🎓 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 is a fundamental operation in programming, allowing data to be ordered in a specific sequence (e.g., ascending or descending). In Java, the Collections framework provides several methods to sort a List, making it easier to manage and manipulate collections of data. This blog post will explore how to sort a List in Java, demonstrating both natural order and custom order sorting.
2. Program Steps
1. Create a List containing elements to be sorted.
2. Sort the List in natural order using the Collections.sort() method.
3. Define a custom Comparator to sort the List in a custom order.
4. Sort the List using the custom Comparator.
5. Display the sorted Lists to illustrate the sorting operations.
3. Code Program
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortList {
public static void main(String[] args) {
// Creating a List of integers
List<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(1);
numbers.add(4);
numbers.add(2);
numbers.add(3);
// Sorting the List in natural order
Collections.sort(numbers);
System.out.println("Sorted in natural order: " + numbers);
// Creating a List of Strings
List<String> fruits = new ArrayList<>();
fruits.add("Banana");
fruits.add("Apple");
fruits.add("Cherry");
fruits.add("Date");
// Sorting the List in custom order (alphabetical order ignoring case)
fruits.sort(String.CASE_INSENSITIVE_ORDER);
System.out.println("Sorted in custom order (case insensitive): " + fruits);
}
}
Output:
Sorted in natural order: [1, 2, 3, 4, 5] Sorted in custom order (case insensitive): [Apple, Banana, Cherry, Date]
Explanation:
1. The program starts by importing the necessary classes from the java.util package. These include ArrayList, Collections, Comparator, and List classes.
2. A List of integers named numbers is created and populated with unsorted values. The List is then sorted in natural order (ascending) using the Collections.sort() method. The sorted List is printed to demonstrate the natural order sorting.
3. A List of Strings named fruits is created and populated with unsorted fruit names. This List is sorted in a custom order, which in this case is alphabetical order ignoring case sensitivity. This is achieved by passing String.CASE_INSENSITIVE_ORDER to the sort() method of the List.
4. The Collections.sort() method is a static method that sorts the specified list into ascending order according to the natural ordering of its elements. For custom order sorting, Lists have a sort() method that accepts a Comparator. In this example, String.CASE_INSENSITIVE_ORDER is used as a Comparator to sort strings in a case-insensitive manner.
5. The output demonstrates the sorted Lists, showing the effectiveness of both natural order sorting and custom order sorting using a Comparator in Java.
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