I was making an array with 10 elements which consist of random values. Now i want to sort them in order of lowest to highest value. How would I write a code for the same? Apr 17, 2018 in Java 182 views

## 3 answers to this question.

```int[] arr = new int;
Random r = new Random();
for (int i = 0; i < arr.length; i++)
arr[i] = r.nextInt(100) + 1;
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
for (int i = arr.length - 1; i >= 0; i--)
System.out.print(arr[i] + " ");
System.out.println();``` answered Apr 17, 2018 by anonymous
```public void bubbleSort(int[] array) {
boolean swapped = true;
int j = 0;
int tmp;
while (swapped) {
swapped = false;
j++;
for (int i = 0; i < array.length - j; i++) {
if (array[i] > array[i + 1]) {
tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
swapped = true;
}
}
}
}``` answered Jul 24, 2018 by
• 6,890 points
```import java.util.Arrays;

public class Sort {
public static void main(String args[])
{
int [] array = new int;
for ( int i = 0 ; i < array.length ; i++ ) {
array[i] = ((int)(Math.random()*100+1));
}
Arrays.sort( array );
for ( int i = 0 ; i < array.length ; i++ ) {
System.out.println(array[i]);
}
}
}``` answered Aug 24, 2018 by
• 4,630 points

+1 vote

## How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

## How to reverse an int array in Java?

for(int i = 0; i < Data.length ...READ MORE

## How to sort an ArrayList of custom object by property in Java?

You can Sort using java 8 yourList.sort(Comparator.comparing(Classname::getName)); or  yourList.stream().forEach(a -> ...READ MORE

## How to convert an int array to string using tostring method in java?

Use java.util.Arrays: String res = Arrays.toString(array); System. ...READ MORE

+1 vote

## What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

## One line initialization of an ArrayList object in Java

In Java 8 or earlier: List<String> string = ...READ MORE

## How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

## How to concatenate two arrays in Java?

public <T> T[] concatenate(T[] a, T[] b) ...READ MORE