how to sort int array in java code example

Example 1: sort array java

import java. util. Arrays;
Arrays. sort(array);

Example 2: how to sort integer array in java

int countOfArray = 5;
int num [] =  new int[counfOfArray];

int num[] = [8 , 5 , 9 , 3 , 4];

for (int i = 0; i < countOfArray; i++) 
        {
            for (int j = i + 1; j < countOfArray; j++) { 
                if (num[i] > num[j]) 
                {
                    temp = num[i];
                    num[i] = num[j];
                    num[j] = temp;
                }
            }
        }

Example 3: java sort int array

// an array of ints
int[] arr = {1, 2, 3, 4, 5, 6};

// an array of reverse sorted ints
int[] arrDesc = Arrays.stream(arr).boxed()
    .sorted(Collections.reverseOrder())
    .mapToInt(Integer::intValue)
    .toArray();

System.out.println(Arrays.toString(arrDesc)); // outputs [6, 5, 4, 3, 2, 1]

Tags:

Java Example