Sorting and reversal of elements

  • Eclipse: Oxygen
  • Java: 1.8

The sorting and reversal of elements from the Collections is shown in the article by the built-in functions in the collection frameworks.

The following example demonstrates the alphabetical sorting of List of country names. sort() method will sort the elements (country names) of the  LinkedList using natural ordering (alphabetically in ascending order).

Output

Element: India

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

*************

Element: Egypt

Element: India

Element: Poland

Element: Russia

Element: Thailand

Element: USA

Following program is the declaration for Collections.reverse() method. It reverses the current order of the list.

Output

Element: India

Element: USA

Element: Poland

Element: Egypt

Element: Thailand

Element: Russia

*************

Element: Egypt

Element: India

Element: Poland

Element: Russia

Element: Thailand

Element: USA

*************

Element: USA

Element: Thailand

Element: Russia

Element: Poland

Element: India

Element: Egypt

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu