How to Sort an Array in Java with Examples
Java
207
sort
6
array
10
Male avatar

loveprogramming viết ngày 27/04/2021

https://grokonez.com/java/how-to-sort-an-array-in-java-tutorial-with-example

How to Sort an Array in Java with Examples

In the tutorial, we will discuss how to Sort an Array with Java in ascending and descending order. java.util.Arrays class provides a lot of Methods to sort an Array with difference types:

  • With Primitives Array, We use the method such as: static void sort(int[] a).
  • With String or Objects Array that implement Comparable, We use method: static void sort(Object[] a).
  • With Custom Object Array that no implement Comparable, We use method: sort(T[] a, Comparator super T> c).

Let's do more details with Java Array Sorting.

Sort with Primitives Java Array

We define a simple Int Java Array:


int[] numbers = new int[] { -9, 5, 4, 8, 11, -2, 2 };

Sorting in Ascending Order

We use the method static void sort(int[] a) to sort the specified array into ascending numerical order.

  • The method returns nothing
  • The Sorting algorithm is a Dual-Pivot Quicksort
  • This algorithm offers O(n log(n)) performance so it is typically faster than traditional (one-pivot) Quicksort implementations.

Practice:

package com.grokonez.sortarray;

import java.util.Arrays;

public class JavaArraySortingExample {
    public static void main(String[] args) {
        int[] numbers = new int[] { -9, 5, 4, 8, 11, -2, 2 };
        
        // Sorting
        Arrays.sort(numbers);
        
        // Print Sorted-Array after Sorting
        for(int i=0; i<numbers.length; i++) {
            System.out.println(numbers[i]); 
        }
    }
}

Output:


// Output
-9
-2
2
4
5
8
11

Sorting in Descending Order

Java doesn’t support the use of Comparators on primitive types so sorting a primitive array in descending order is not easy.

More:

https://grokonez.com/java/how-to-sort-an-array-in-java-tutorial-with-example

How to Sort an Array in Java with Examples

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

444 bài viết.
77 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/kotlin/kotlinarraysortsortbysortwith Kotlin Array sort(), sortBy(), sortWith() In the tutorial, JavaSampleApproach will guid...
loveprogramming viết 16 ngày trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
444 bài viết.
77 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!