1024programmer Java Arrays.sort() in Java, example

Arrays.sort() in Java, example

Arrays.sort() in Java, examples

Original text: https://www . geeksforgeeks . org/arrays-sort-in-Java-with-examples/

An array class is a class that contains static methods that are used on arrays to search, sort, compare, insert elements, or return a string representation of the array. in an array. So let us specify the function first and later we will discuss the same. They appear in the java.util.Arrays class as shown below. Here we will discuss different graphs using sort() method of array class.

The Arrays.sort() method consists of two variants, one is that we do not pass any parameters, it sorts the entire array, whether it is an integer array or a character array, but if we want to use Arrays This method of the class sorts a specific section, then we override it and pass the start and last index to the array.

Syntax:Sort() method

Arrays .sort();

Syntax:Overloaded sort() method

public static void sort(int[] arr , int from_Index, int to_Index) ;

Parameters:It can be seen from the syntax that it has three parameters, as follows:

  • The array to sort.
  • The index (inclusive) of the first element to sort (called from_index)
  • The index (excluding) of the last element to sort (called last_index)

Return type:Does not return a value.

Now let us look at the implementation of the sort() function in different scenarios of the Arrays class, as follows:

Example 1:

Java

// Java Program to Sort Array of Integers
// by Default Sorts in an Ascending Order
// using Arrays.sort() Method
// Importing Arrays class from the utility class
import java.util.Arrays;
// Main class
public class GFG {
 / / Main driver method
 public static void main(String[] args)
 {
 // Custom input array
 int[] arr = { 13, 7, 6, 45, 21, 9, 101, 102 };
 // Applying sort() method over to above array
 // by passing the array as an argument
 Arrays.sort(arr);
 // Printing the array after sorting
System.out. span>println("Modified arr[] : %s",
                                                                                          (arr));
 }
}

Output:

Modified arr[] : [6, 7, 9, 13, 21, 45 , 101, 102]

Example 2:

Java

// Java program to Sort a Subarray in Array
// Using Arrays.sort() method
// Importing Arrays class from java.util package
import java.util.Arrays;
/ / Main class
public class GFG {
// Main driver method
public static void main( String[] args)
 {
 // Custom input array
  // It contains 8 elements as follows
 int[] arr = { 13, 7, 6, 45, 21, 9, 2 , 100 };
 // Sort subarray from index 1 to 4, i.e.,
 // only sort subarray {7, 6, 45, 21} and
// keep other elements as it is.
 Arrays. sort(arr, 1, 5) ;
 // Printing the updated array which is
 // sorted after 2 index inclusive till 5th index
 System.out.println("Modified arr[] : %s ",
                                                                ="na">toString(arr));
 }
}

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/arrays-sort-in-java-example/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索