<div dir="ltr" style="text-align: justify;" trbidi="on">We can sort the elements of:</p>
<ol>
<li><b>String objects</b></li>
<li><b>Wrapper class objects</b></li>
<li><b>User-defined class objects</b></li>
</ol>
<p><b>Collections </b>class provides static methods for sorting the elements of collection.If collection elements are of <i><b>Set </b></i>type, we can use <i><b>TreeSet</b></i>. But We cannot sort the elements of <i><b>List</b></i>. <i><b>Collections </b></i>class provides methods for sorting the elements of List type elements.<br />
<b>Method of <i>Collections </i>class for sorting <i>List </i>elements</b><br />
<i><b>public void sort(List list): </b></i>is used to sort the elements of <i><b>List</b></i>. <i><b>List</b></i> elements must be of <i><b>Comparable </b></i>type.</p>
<div align='center' id="ads-id"></div>
<p><b>Note:</b> <i><b>String </b></i>class and <i><b>Wrapper </b></i>classes implements the <i><b>Comparable </b></i>interface. So if you store the objects of string or wrapper classes, it will be <i><b>Comparable</b></i>.</p>
<p><b>Example of Sorting the elements of <i>List </i>that contains string objects-</b></p>
<pre class="highlight" name="code">import java.util.*; 
class Simple12{ 
public static void main(String args[]){ 
 
ArrayList al=new ArrayList(); 
al.add("Dinesh"); 
al.add("Sweetu"); 
al.add("Adesh"); 
al.add("Vinesh"); 
 
Collections.sort(al); 
Iterator itr=al.iterator(); 
 
while(itr.hasNext()){ 
System.out.println(itr.next()); 
 } 
} 
 
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
Adesh<br />
Dinesh<br />
Sweetu<br />
Vinesh</div>
<p>
<b>Example of Sorting the elements of <i>List </i>that contains <i>Wrapper</i> class objects</b></p>
<pre class="highlight" name="code">import java.util.*; 
class Simple12{ 
public static void main(String args[]){ 
 
ArrayList al=new ArrayList(); 
al.add(Integer.valueOf(201)); 
al.add(Integer.valueOf(101)); 
al.add(230);//internally will be converted into objects as Integer.valueOf(230) 
 
Collections.sort(al); 
 
Iterator itr=al.iterator(); 
while(itr.hasNext()){ 
System.out.println(itr.next()); 
 } 
} 
} 
</pre>
<p></p>
<div style="background-color: #ff99cc;"><b>Output:</b><br />
101<br />
201<br />
230</div>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<b>Java Collections Tutorial</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/arraylist-class-in-java-collection/">ArrayList class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedlist-class-in-collection/">LinkedList class</a></b></li>
<li><b><a href="https://dineshonjava.com/listiterator-interface-in-collection/">ListIterator interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashset-class-in-collection/">HashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashset-class-in-collection/">LinkedHashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/treeset-classin-collection/">TreeSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashset-and-treeset/">Difference between TreeSet &; HashSet</a></b></li>
<li><b><a href="https://dineshonjava.com/map-interface-in-collection/">Map interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashmap-class-in-collection-framework/">HashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/how-does-java-hashmap-work-internally/">How does work HashMap?</a> </b></li>
<li><b><a href="https://dineshonjava.com/difference-between-treemap-vs-hashmap/">Difference between TreeMap vs HashMap</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashmap-class-in-collection/">LinkedHashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/treemap-class-in-collection-framework/">TreeMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Hashtable class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashmap-and/">Difference between HashMap and HashTable in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/sorting-in-collection-framework/">Sorting</a></b></li>
<li><b><a href="https://dineshonjava.com/java-comparable-and-comparator/">Comparable interface</a></b></li>
</ol>
</div>
<p></p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/difference-between-hashmap-and/">Previous</a> <;<; ; ; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>||  ; >;>;<a href="https://dineshonjava.com/sorting-in-collection-framework/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/difference-between-hashmap-and/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/java-collections-interview-questions/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '407'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…