sort array java
import java. util. Arrays;
Arrays. sort(array);
sort array java
import java. util. Arrays;
Arrays. sort(array);
sorting java
public class SortingData {
public static void main(String[] args)
{
int[] arr = { 13, 7, 6, 45, 21, 9, 101, 102 };
Arrays.sort(arr);//sort() function
System.out.printf("Modified arr[] : %s",
Arrays.toString(arr));
}
}
Arrays.sort() in java
// java sort arraylist
import java.util.ArrayList;
import java.util.Collections;
public class JavaSortArraylist
{
public static void main(String[] args)
{
ArrayList<String> al = new ArrayList<String>();
al.add("Bear");
al.add("Fox");
al.add("Deer");
al.add("Cheetah");
al.add("Anteater");
al.add("Elephant");
System.out.println("Before sorting ArrayList: " + al);
Collections.sort(al);
System.out.println("After sorting ArrayList in Ascending order: " + al);
}
}
Arrays.sort() in java
// java program to sort strings in an alphabetical order
import java.util.Arrays;
public class SortStringAlphabeticalOrder
{
public static void main(String[] args)
{
String[] strAsc = {"Bear","Fox","Deer","Cheetah","Anteater","Elephant"};
System.out.println("Before sorting: ");
for(String string : strAsc)
{
System.out.println(string);
}
// arrays.sort
Arrays.sort(strAsc);
System.out.println("-----------------------------------");
System.out.println("After sorting: ");
for(String str : strAsc)
{
System.out.println(str);
}
}
}
Arrays.sort() in java
// how to sort an array of strings in descending
import java.util.Arrays;
import java.util.Collections;
public class SortStringAlphabeticalOrder
{
public static void main(String[] args)
{
String[] strDesc = {"Bear","Fox","Deer","Cheetah","Anteater","Elephant"};
System.out.println("Before sorting: ");
for(String string : strDesc)
{
System.out.println(string);
}
// arrays.sort
Arrays.sort(strDesc, Collections.reverseOrder());
System.out.println("-----------------------------------");
System.out.println("Sorting string array in descending or reverse order: ");
for(String string : strDesc)
{
System.out.println(string);
}
}
}
how to sort array java
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();
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us