Add the Line before println and your array gets sorted

Arrays.sort( array );
Answer from rauschen on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › arrays-sort-in-java
Arrays.sort() in Java - GeeksforGeeks
It sorts arrays in ascending order by default (for primitives and Comparable objects). For objects, you can customize sorting using Comparator. It uses Dual-Pivot QuickSort for primitives and TimSort for objects (optimized for performance).
Published   May 13, 2026
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Arrays.html
Arrays (Java Platform SE 8 )
April 21, 2026 - Sorts the specified range of the array into ascending order. The range to be sorted extends from the index fromIndex, inclusive, to the index toIndex, exclusive.
Discussions

Sort an array in Java - Stack Overflow
I'm trying to make a program that consists of an array of 10 integers which all has a random value, so far so good. However, now I need to sort them in order from lowest to highest value and then ... More on stackoverflow.com
🌐 stackoverflow.com
Quick and dirty method to sort array using method in java without using Array.sort() method?
Whatever problem you're having, it likely has nothing to do with the "pass by value nature of Java". Java is technically a "pass-by-value" language, but when you pass an object (including an array), the value you're passing is itself a reference to the object, not the object itself. The array is not copied, only the reference is copied. So changes to an array inside a method are reflected outside of the method. And so the code you posted does in fact work to sort a list of scores in descending order, even if it's placed inside a method: https://godbolt.org/z/PrhscbsPG If you're having some problems with your code, please be more specific about what you're doing, what you expect to happen and what you're seeing instead. More on reddit.com
🌐 r/learnprogramming
5
2
October 22, 2024
Sorting in Java in descending order
Write your own swapping function. Swap after arrays.sort. More on reddit.com
🌐 r/leetcode
5
1
June 9, 2022
Sorting array list without using .sort in Java
Okay. Why does it do that? You have the code in front of you. You can trace it through, line by line. What happens at each step? What does the bag look like before you add the item? Where should the item go? If you know how to use a debugger, do so. If you don't know how to use a debugger then start adding println statements to the code so that you know exactly what is going on. print out everything you can think of. Your code is doing things in an odd way, IMHO. I would normally expect that you'd skip over the items with a weight of less than the new item and then, when you find an item that is heavier than yours, "move up" everything after that to make space for yours. You start off by putting your item in the very first position, which seems very wrong to me. Your next question is probably going to be "How do I fix this code?". TBH, I don't think you "fix" this so much as rewrite it from scratch. Write down a clear set of instructions, stating what the function will do. Super detailed. Then implement that. More on reddit.com
🌐 r/learnprogramming
8
1
October 2, 2022
🌐
W3Schools
w3schools.com › java › ref_arrays_sort.asp
Java Arrays sort() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The sort() method sorts an array in ascending order.
🌐
DataCamp
datacamp.com › doc › java › sort
Java Array sort()
In this example, the Arrays.sort() method sorts the numbers array in ascending order. The output will be [1, 2, 3, 5, 8]. import java.util.Arrays; public class SortStringArray { public static void main(String[] args) { String[] fruits = {"Banana", "Apple", "Cherry", "Date"}; Arrays.sort(fruits); ...
🌐
Medium
medium.com › @reetesh043 › collection-sort-vs-arrays-sort-in-java-1528f83223c3
Medium
February 21, 2024 - The Arrays.sort() method automatically selects the appropriate sorting algorithm based on the type of data it receives, in this case, a primitive array. After sorting, we print the sorted array using Arrays.toString(). ... import java.util.Arrays; ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-sort-the-elements-of-an-array-in-ascending-order
Java Program to Sort the Elements of an Array in Ascending Order - GeeksforGeeks
July 23, 2025 - ... // Java Program to sort the ... main(String[] args) { int[] arr = new int[] { -2, 0, 1, 3, -1, 2 }; // use Arrays.sort() method to sort array // elements in ascending order Arrays.sort(arr); System.out.println(Arrays....
🌐
CodeGym
codegym.cc › java blog › java algorithms › how to sort an array in java
How to Sort an Array in Java
January 7, 2025 - Let's start with the simplest. Someone has already written a method for sorting arrays in Java for us. This method is in the Arrays class, more specifically java.util.Arrays. This class contains various methods for working with arrays, such as sorting and searching.
🌐
Florida State University
cs.fsu.edu › ~myers › cop3252 › notes › sorting.html
Sorting Arrays
There are a number of sort() methods for sorting arrays of built-in types
🌐
Baeldung
baeldung.com › home › java › java array › sorting arrays in java
Sorting Arrays in Java | Baeldung
January 8, 2024 - Java’s util.Arrays.sort method provides us with a quick and simple way to sort an array of primitives or objects that implement the Comparable interface in ascending order.
🌐
PREP INSTA
prepinsta.com › home › dsa in java › java program for sorting elements in an array
Java program for Sorting Elements In An Array
August 8, 2025 - This article covers multiple sorting methods in Java with complete code examples, theoretical explanations, and complexity analysis. What is Sorting? Sorting refers to the process of arranging data in a specific order, typically in ascending or descending order. Sorting makes data easier to search, analyze, and visualize. Sorting algorithms differ based on performance, memory usage, and the way they handle equal elements (stability). Example Problem Statement: Given an array of integers, sort the elements in ascending order using different sorting techniques in Java.
🌐
Hero Vired
herovired.com › learning-hub › topics › how-to-sort-an-array-in-java
How to Sort an Array in Java - Complete Tutorial
July 23, 2024 - The other way to sort an array is to create a user-defined method. This method contains the sorting logic and can be reused multiple times in a program. ... // Java program to sort integer array in ascending order using user-defined method public class SortArrayExample { public static void main(String[] args){ // Define an integer type array int[] arr = new int[] {99, 39, 10, -9, 20, 2990, 0, -1, 13}; // Printing the original array System.out.println("The Original Array is: "); for (int num : arr) { System.out.print(num + " "); } // Invoking user-defined method sortArrayInAsc(arr, arr.length);
🌐
freeCodeCamp
freecodecamp.org › news › how-to-sort-a-list-in-java
How to Sort a List in Java – Java List Sorting Example
January 24, 2023 - We call this process sorting. Sorting refers to arranging data in a specific order using certain criteria. You can sort different types of data, including numbers, strings, and objects. Java provides built-in methods for sorting, such as the Collections classes.
🌐
VisuAlgo
visualgo.net › en › array
Array - VisuAlgo
Array is built-in almost all programming languages, e.g., C++, Python ('array' is called as 'list' in Python), Java, etc. We can use (Compact) Array to implement List ADT. We can use (Compact) Array to solve many classic problems. When not being used as a List ADT implementation (where positional order matters), it is often beneficial to first · sort the elements first so that we can utilize faster algorithms.
🌐
TutorialsPoint
tutorialspoint.com › java › util › arrays_sort_short_index.htm
Java.util.Arrays.sort() Method
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo { public static void main(String[] args) { // initializing unsorted short array short sArr[] = {3, 13, 1, 9, 21}; // let us print all the elements available in list for (short number : sArr) { System.out.println("Number = " + number); } // sorting array from index 1 to 4 Arrays.sort(sArr, 1, 4); // let us print all the elements available in list System.out.println("short array with some sorted values(1 to 4) is:"); for (short number : sArr) { System.out.println("Number = " + number); } } }
🌐
Hyperskill
hyperskill.org › university › java › sorting-arrays-and-lists-in-java
Sorting Arrays and Lists in Java
November 22, 2024 - We can use any comparison-based sorting algorithm for this problem, but since we are sorting by length, we can use a modified version of quick sort that chooses the pivot based on the length of the strings rather than their values. This approach is called quick sort with median-of-three partitioning and has a time complexity of O(nlogn) in the average case. ... Suppose we have an array of integers that we want to sort in descending order which contains only 20 elements.
🌐
Reddit
reddit.com › r/learnprogramming › quick and dirty method to sort array using method in java without using array.sort() method?
r/learnprogramming on Reddit: Quick and dirty method to sort array using method in java without using Array.sort() method?
October 22, 2024 -

My previous research:

https://stackoverflow.com/questions/8938235/sort-an-array-in-java

I've code for it but I ma having tough time implementing it as a method because of pass by value nature of java.

// some sorting technique
for (int i = 0; i < scores.length; i++) {
for (int j = i; j < scores.length; j++) {
if (scores[j] > scores[i]) {
int temp = scores[i];
scores[i] = scores[j];
scores[j] = temp;
// swap names as well
String temp2 = names[i];
names[i] = names[j];
names[j] = temp2;
}
}
}

Implementing this as a method seems really troublesome because the changes done inside method aren't reflected outside of method

🌐
Medium
medium.com › @AlexanderObregon › javas-arrays-sort-explained-d7a2d48f1cc0
Java’s Arrays.sort() Explained | Medium
September 23, 2024 - After partitioning the array into three parts, it recursively applies the same sorting strategy to each part. This leads to efficient division of work and reduces the number of necessary comparisons. Combining Results: After sorting the three partitions, the final sorted array is produced by combining the results of these recursive steps. To demonstrate how Java...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-sort-an-array-of-strings-in-java
How to sort an Array of Strings in Java - GeeksforGeeks
July 12, 2025 - import java.util.Arrays; import java.util.Collections; // Driver Class public class SortExample { // main method public static void main(String[] args) { String arr[] = { "practice.geeksforgeeks.org", "www.geeksforgeeks.org", "code.geeksforgeeks.org" }; // Sorts arr[] in ascending order Arrays.sort(arr); System.out.printf("Modified arr[] : \n%s\n\n", Arrays.toString(arr)); // Sorts arr[] in descending order Arrays.sort(arr, Collections.reverseOrder()); System.out.printf("Modified arr[] : \n%s\n\n", Arrays.toString(arr)); } }