How to sort a list in java – Best [5] Ways

Welcome to HubSpotint, In Today’s article, we will see How We can Sort The List. Here We will Discuss How to sort a list in java Using basic methods and also We will Sort the list Using a Comparator and Comparable Interface. After that, We will discuss How we can create a Pojo Class and sort a List of that Pojo Class.

As we All know a List is very similar to Array but one big difference between the list and an Array is Arrays are fixed-length means you can not increase or decrease the size of the array once it is created the List is Dynamic means the size of the list automatically Increase or Decrease once the element is added to the List.

The list is an Interface Which Extends the Collection Interface. The Classes which Implement the List Interface are LinkedList, ArrayList, Vector and Stack. Here we will use the class ArrayList to implement all the sorting methods of the list.

Sorting Java List

1. how to sort ArrayList in java using Collections.sort()

Sometimes we need to sort the list before processing the elements of the lists. For this, we can use Collections.sort() method. Collections.sort() method provide the ascending natural ordering of sorted element. Internally it uses the Comparable Interface for sorting the list.

Now we will see the Example…

How to sort a list in java

output for sorting list
output for sorting list

Now, if you want to copy the code, you can Do it From Here…

public class HubSpotingListSort {
       public static void main(String[] args) {

      // Create the list
      List<Integer> hubSpotList = new ArrayList<>();

      // add the element in the hubspoting list
      hubSpotList.add(23);
      hubSpotList.add(12);
      hubSpotList.add(45);
      hubSpotList.add(98);
      hubSpotList.add(10);
      hubSpotList.add(56);

      //sort the list using Collections.sort() method
      Collections.sort(hubSpotList);

      // now print the hubspotingList using ForEach Loop
      for(Integer obj : hubSpotList)
      {
          System.out.print(obj+" ");
      }  
   }
}

As you have seen how we can Sort the Integer List Using the Collections.sort() method. Similarly, You can Also Sort the String List using this method example…

public class HubSpotingListSort {
          
                 public static void main(String[] args) {

      // Create the list
      List<String> hubSpotList = new ArrayList<>();

      // add the element in the hubspoting list
      hubSpotList.add("Ramesh");
      hubSpotList.add("Devid");
      hubSpotList.add("Tim");
      hubSpotList.add("Warner");
      hubSpotList.add("lucy");
      hubSpotList.add("mac");

      //sort the list using Collections.sort() method
      Collections.sort(hubSpotList);

      // now print the hubspotingList using ForEach Loop
      for(String obj : hubSpotList)
      {
          System.out.print(obj+" ");
      }  
   }
}

2. sort a list in java using Comparator

We can also sort the list Either in ascending order or Descending Order Using the Comparator Interface…please see the Implementation below.

sort arraylist in java
sort arraylist in java

You Can Copy the Code From Here…

public class HubSpotingListSort {
     public static void main(String[] args) {

      // Create the list
      List<String> hubSpotList = new ArrayList<>();

      // add the element in the hubspoting list
      hubSpotList.add("Ramesh");
      hubSpotList.add("Devid");
      hubSpotList.add("Tim");
      hubSpotList.add("Warner");
      hubSpotList.add("lucy");
      hubSpotList.add("mac");

      //sort the list using Collections.sort() method --- using lambda Expressions
      Collections.sort(hubSpotList, (o1,o2) -> o1.compareTo(o2));

      // now print the hubspotingList using ForEach Loop
      for(String obj : hubSpotList)
      {
          System.out.print(obj+" ");
      }  
  }
}

3. How to sort a list in java using Stream API

This is the third way of sorting the list. we can sort the list using stream API in java 8 also. It is a very efficient way of sorting lists using the Sorted() method. We can also sort the list in reverse order using the Comparator in the Stream API. let’s See the Example.

arraylist
sort arraylist in java
public class HubSpotingListSort {
        public static void main(String[] args) {

      // Create the list
      List<Integer> hubSpotList = new ArrayList<>();

      // add the element in the hubspoting list
      hubSpotList.add(11);
      hubSpotList.add(56);
      hubSpotList.add(23);
      hubSpotList.add(29);
      hubSpotList.add(78);
      hubSpotList.add(69);

      //Sort List the using the Stream API 
      hubSpotList.stream().sorted().forEach(obj -> System.out.print(obj + " "));      
    }
}

4. sort a list in java Using the NaturalOrder and Comparator

we can also sort the list using the naturalOrder and Comparator please look at the example…

sort list

Output

How to sort a list in java
How to sort a list in java

If you want to copy the code you can copy it below…

public class HubSpotingListSort {
     public static void main(String[] args) {

      // Create the list
      List<Integer> numbers = new ArrayList<>();

      // add the element in the hubspoting list
      numbers.add(11);
      numbers.add(56);
      numbers.add(23);
      numbers.add(29);
      numbers.add(78);
      numbers.add(69);

      //sort the list using the Comparator.naturalOrder()
      numbers.sort(Comparator.naturalOrder());

      //print the element from list
      for(Integer obj : numbers) {
          System.out.print(obj + " ");
      }

   }
}

Final Verdict on How to sort a list in java:

This is all from my side about this articleHow to sort a list in java, coming with some more knowledge soon. Look at these blog’s to ensure you’re on the right path to your digital success. 

Look at these services you may need for your website.

Do you have any questions Regarding How to sort a list in java

Drop your queries and comments section below, or post them on HubSpoting Forums, and we will be happy to answer.

Disclaimer: 

Our content is reader-supported and friendly. This means if you click on any one of our links, then we may earn a commission. We are so happy that you support us.

Leave a Comment