Tuesday, October 13, 2015

java - How to sort an array

MainActivity.java

package com.cfsuman.me.javaexamples;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import java.util.Arrays;
import java.util.Collections;


public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get widget reference from XML layout
        TextView tv = (TextView) findViewById(R.id.tv);

        // Initialize a new String array
        String[] colors = new String[]{"Black","Yellow","AliceBlue","Red","Green"};

        tv.setText("String array : ");
        // Iterate over the String array
        for(int i=0;i<colors.length;i++){
            tv.setText(tv.getText() + colors[i]+ ", ");
        }

        // Sort the String array in ascending order
        Arrays.sort(colors);

        tv.setText(tv.getText() + "\nString array (sorted ascending): ");
        // Iterate over the ascending order sorted String array
        for(int i=0;i<colors.length;i++){
            tv.setText(tv.getText() + colors[i]+ ", ");
        }

        // Sort the String array in descending order
        Arrays.sort(colors, Collections.reverseOrder());

        tv.setText(tv.getText() + "\nString array (sorted descending): ");
        // Iterate over the descending sorted String array
        for(int i=0;i<colors.length;i++){
            tv.setText(tv.getText() + colors[i]+ ", ");
        }

        // Initializing a new int array
        // This is a primitive type array
        int[] numbers = new int[]{20,10,30,50,40};

        tv.setText(tv.getText() + "\n\nInt array (primitive type) : ");
        // Loop through the int array
        for(int i=0;i<numbers.length;i++){
            tv.setText(tv.getText() + "" + numbers[i]+ ", ");
        }

        // Sort the int array elements in ascending order
        Arrays.sort(numbers);

        tv.setText(tv.getText() + "\nInt array (ascending sorted) : ");
        // Loop through the int array
        for(int i=0;i<numbers.length;i++){
            tv.setText(tv.getText() + "" + numbers[i]+ ", ");
        }

        /*
            Sort an int array to descending order
            It is a primitive type array, so there is no direct way to sort it descending order
            At first we need to sort the array as ascending order
            Then we need to reverse the sorted array elements
         */
        Arrays.sort(numbers);
        int temp;
        for(int i=0;i<numbers.length/2;i++){
            temp = numbers[i];
            numbers[i] = numbers[numbers.length-1-i];
            numbers[numbers.length-1-i] = temp;
        }

        tv.setText(tv.getText() + "\nInt array (descending sorted) : ");
        // Loop through the descending sorted int array
        for(int i=0;i<numbers.length;i++){
            tv.setText(tv.getText() + "" + numbers[i]+ ", ");
        }

        // Initialize a new Integer array
        Integer[] marks = new Integer[]{29,19,79,59,49};

        tv.setText(tv.getText() + "\n\nInteger array : ");
        // Loop through the Integer array
        for(int i=0;i<marks.length;i++){
            tv.setText(tv.getText() + "" + marks[i]+ ", ");
        }

        // Sort the Integer array in ascending order
        Arrays.sort(marks);

        tv.setText(tv.getText() + "\nInteger array (ascending sorted): ");
        // Loop through the ascending sorted Integer array
        for(int i=0;i<marks.length;i++){
            tv.setText(tv.getText() + "" + marks[i]+ ", ");
        }

        // Simply sort the Integer array in descending order
        Arrays.sort(marks,Collections.reverseOrder());

        tv.setText(tv.getText() + "\nInteger array (descending sorted): ");
        // Loop through the descending sorted Integer array
        for(int i=0;i<marks.length;i++){
            tv.setText(tv.getText() + "" + marks[i]+ ", ");
        }
    }
}
More android examples