Sunteți pe pagina 1din 2

Sorting of ArrayList<String>

import java.util.*;

public class Main

public static void main(String[] args) {

ArrayList<String> countries = new ArrayList<>();

countries.add("India");

countries.add("US");

countries.add("China");

countries.add("Denmark");

System.out.println("The elements of the array before sorting: ");

for(String country: countries)

System.out.println(country);

Collections.sort(countries);

System.out.println("The elements of the array after sorting alphabetically: ");

for(String country: countries)

System.out.println(country);

Collections.sort(countries, Collections.reverseOrder());

System.out.println("The elements of the array after sorting in reverse order: ");

for(String country: countries)

System.out.println(country);

}
Sorting of ArrayList<Integer>
import java.util.*;

public class Main

public static void main(String[] args) {

ArrayList<Integer> numbers = new ArrayList<>();

numbers.add(12);

numbers.add(15);

numbers.add(456);

numbers.add(1234);

System.out.println("The elements of the array before sorting: ");

for(Integer number: numbers)

System.out.println(number);

Collections.sort(numbers);

System.out.println("The elements of the array after sorting alphabetically: ");

for(Integer number: numbers)

System.out.println(number);

Collections.sort(numbers, Collections.reverseOrder());

System.out.println("The elements of the array after sorting in reverse order: ");

for(Integer number: numbers)

System.out.println(number);

S-ar putea să vă placă și