Sorting ArrayLists

Start course
Overview
Difficulty
Beginner
Duration
2h 16m
Students
26
Description

In this course, we'll learn the Collection framework and the Map Interface.

Learning Objectives

  • Collection Framework vs Array
  • Hierarchy of Collection Framework
  • List interface - ArrayList Class
  • Iterator Interface
  • Set Interface
  • Queue and Dequeue Interfaces
  • Map Interface

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this lesson, we'll learn to sort the elements of an ArrayList from smallest to largest i.e., ascending. It's the same as sorting array that we learned in previous lessons, only the method is used different. We will use the sort() method of the Collections class. If you're ready, let's move on to the Eclipse and get some practice. In the Collections project, I right click on the 'collectionsexample' package and select the New, Class. The class name can be SortingArrayList. And check the checkbox for the main method and click the 'Finish' button. Let's look at the sorting of the integer ArrayList. I'll create an integer ArrayList, ArrayList< Integer > numbers = new ArrayList<>(). Let's add some elements to this list. The first element can be six. I'll copy and paste this line five times. This can be four, this can be 11, this can be 32, this can be two, and the last element can be 50.

Now, let's sort the elements of this list using the sort() method of the Collections class. Here, I write Collections.sort. And in parentheses (), I write the name of the list i.e., numbers. Let's print the numbers list on the console, System.out.println(numbers). And let's run the code and see. As you can see, the elements are sorted in ascending order: 2, 4, 6, 11, 32, 50. Now, let's sort the char list. The name of the list can be myChars. Let's add some elements to this list. The first element will be a lowercase a. I'll copy and paste this line five times. This will be B, this will be A, this will be F, this will be b, this will be E. Let's sort it. Collections.sort(myChars). And let's print the elements of this list on the console, System.out.println(myChars). Let's run the code and test it. Notice that it sorts the uppercase letters first, then the lowercase letters, because capital letters start first according to the ASCII table. 

Let's sort the string list. The name of the list can be myCars. Now, let's add the elements of this list. The first element can be Ferrari. And I'll copy this line and paste it five times. This will be Opel, the third will be Mercedes, fourth element will be BMW, fifth element will be Ford, and the last element will be hyundai. The first letter of hyundai is lowercase. Let's sort it, Collections.sort(myCars). And let's print the elements of this list on the console, System.out.println(myCars). Let's run the code and test it. This time, they sorted in alphabetical order. Alphabetical order is taken into account when sorting strings. But as you can see, the hyundai is the last element because it begins with a lowercase letter, and the lowercase is always greater than the uppercase letter in sorting.

Let's do another example of sorting strings. I'll copy this list and paste it below. The name of the list can be myNums. And its elements can be 50, 70, 5, 90, 9, 60. And this will be myNums. And this will also be myNums. Let's try to guess the result before running the code. This time our string consists of numbers, not letters. So, the logic here is it looks at the first digits, starting from the left first. So, five for the first element, seven for the second element, five for the third element, nine for the fourth element, nine again for the fifth element, and six for the last element. It sorts them first. In this case, the first two elements will be 5 or 50. The third element will be 60. The fourth element will be 70. And the fifth and sixth elements will be 9 or 90. If we look at 5 and 50, since they start with the same number from the left, they are both 5. In this case, it looks at the second number from the left. 

But since 5 consists of a single digit, it has no equivalent in the second digit. So, 5 is smaller. So, the first two elements will be 5 and 50 respectively. Similarly, when we create the last two elements, 9 becomes less than 90. So, the fifth element would be 9, and the last element would be 90. I also explained this logic in the sorting array class. It's been a repetition for you, but I wanted to remind you of this issue once again because it's an important and confusing subject. Now, let's run the application and test it. Sorry, I forgot to change the name of the list, so the myNums list is currently empty. Let's fix this. I'll change these to the myNums. Now, we can run it again.

As you can see, first 5, then 50, then 60, then 70, then 9, and finally 90 were printed. Also, let's add two more elements to this list. The seventh element will be lowercase a and the eighth element will be capital Z. So, if the string lists contain letters and numbers, this time the letters will sort after the numbers and the rule is always capital letter is less than the lowercase letter in sorting. So, the seventh element must be Z and the last element must be a. Let's run and see. As you can see, the seventh element is uppercase Z and the eighth element is lowercase a. The thing to keep in mind is this: if the array or list consists of numeric values, it's sorted from largest to smallest. If they consist of characters, first uppercase letters then lowercase letters are sorted. If the type of array or list is the string, it's sorted alphabetically. However, if the elements in a string represent numeric numbers, this time it compares the numbers among themselves on the condition that it starts from the left and sorts the elements of the list and array accordingly. Yes, the sorting elements are like this. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
409
Courses
35
Learning Paths
2

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics