Sort ArrayList in ascending and descending order in Java Android

How to display integer array list values on screen and sort using Collections.sort() and reverse() method.

In this tutorial we are using Collections.sort() and Collections.reverse() methods to automatically convert normal array list into ascending and descending order. So here is the complete step by step tutorial for Sort ArrayList in ascending and descending order in Java Android.

android-project-download-code-button

How to Sort ArrayList in ascending and descending order in Java Android.

Code for MainActivity.java file.

 package com.sortarraylist_android_examples.com;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

 TextView textview1,textview2,textview3;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview1 = (TextView)findViewById(R.id.textView1);
 textview2 = (TextView)findViewById(R.id.textView2);
 textview3 = (TextView)findViewById(R.id.textView3);
 
 //Initializing integer array list;
 List<Integer> integerList = new ArrayList<Integer>();
 
 //Dynamically adding values to integer array list.
 
 integerList.add(1);
 integerList.add(3);
 integerList.add(5);
 integerList.add(7);
 integerList.add(2);
 integerList.add(4);
 integerList.add(6);
 
 //Printing integer array list without sorting values.
 
 for(int i=0; i < integerList.size(); i++){
 
 textview1.setText(textview1.getText() + " " + integerList.get(i) + " , ");
 }
 
 //Sort array list into Ascending order.
 
 Collections.sort(integerList);
 
 //Printing integer array list into Ascending order.
 
 for(int i=0; i < integerList.size(); i++){
 
 textview2.setText(textview2.getText() + " " + integerList.get(i) + " , ");
 }
 
 //Sort array list into Descending order.
 
 Collections.reverse(integerList);
 
 //Printing integer array list into Descending order.
 
 for(int i=0; i < integerList.size(); i++){
 
 textview3.setText(textview3.getText() + " " + integerList.get(i) + " , ");
 }
 
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.sortarraylist_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="154dp"
 android:text=""
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <TextView
 android:id="@+id/textView2"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text=""
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

 <TextView
 android:id="@+id/textView3"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/textView2"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="23dp"
 android:text=""
 android:textAppearance="?android:attr/textAppearanceLarge"
 android:gravity="center" />

</RelativeLayout>

Screenshot:

Sort ArrayList in ascending and descending order in Java Android

Click here to download Sort ArrayList in ascending and descending order in Java Android project with source code.