Add the Line before println and your array gets sorted
Arrays.sort( array );
Answer from rauschen on Stack OverflowSort an array in Java - Stack Overflow
Quick and dirty method to sort array using method in java without using Array.sort() method?
Sorting in Java in descending order
Sorting array list without using .sort in Java
Videos
Add the Line before println and your array gets sorted
Arrays.sort( array );
Loops are also very useful to learn about, esp When using arrays,
int[] array = new int[10];
Random rand = new Random();
for (int i = 0; i < array.length; i++)
array[i] = rand.nextInt(100) + 1;
Arrays.sort(array);
System.out.println(Arrays.toString(array));
// in reverse order
for (int i = array.length - 1; i >= 0; i--)
System.out.print(array[i] + " ");
System.out.println();
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