How to sort a Java array in ascending order?

You can use the Arrays.sort() method to sort a Java array in ascending order. An example is shown below:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] arr = {5, 3, 9, 1, 7};

        // 对数组进行从小到大排序
        Arrays.sort(arr);

        // 输出排序后的数组
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

Running the above code will output: 1 3 5 7 9, indicating that the array has been sorted in ascending order.

bannerAds