1// java sort arraylist
2import java.util.ArrayList;
3import java.util.Collections;
4public class JavaSortArraylist
5{
6 public static void main(String[] args)
7 {
8 ArrayList<String> al = new ArrayList<String>();
9 al.add("Bear");
10 al.add("Fox");
11 al.add("Deer");
12 al.add("Cheetah");
13 al.add("Anteater");
14 al.add("Elephant");
15 System.out.println("Before sorting ArrayList: " + al);
16 Collections.sort(al);
17 System.out.println("After sorting ArrayList in Ascending order: " + al);
18 }
19}
1enum Code {
2 Str1(1), Str2(2), Str3(3), Str4(4), Str5(5));
3
4 int sortNumber;
5
6 Code(int sortNumber) {
7 this.sortNumber = sortNumber;
8 }
9
10 int returnNumber() {
11 return sortNumber;
12 }
13};
14
15public static void main(String[] args) {
16 List<Object> obj = new ArrayList<Object>();
17
18 Collections.sort(obj, new Comparator<Object>() {
19 @Override
20 public int compare(Object object1, Object object2) {
21 return Code.valueOf(object1.getStr()).returnNumber() > Code.valueOf(object2.getStr()).returnNumber() ? 1 : -1;
22 }
23 });
24}
1// how to sort an array of strings in descending
2import java.util.Arrays;
3import java.util.Collections;
4public class SortStringAlphabeticalOrder
5{
6 public static void main(String[] args)
7 {
8 String[] strDesc = {"Bear","Fox","Deer","Cheetah","Anteater","Elephant"};
9 System.out.println("Before sorting: ");
10 for(String string : strDesc)
11 {
12 System.out.println(string);
13 }
14 // arrays.sort
15 Arrays.sort(strDesc, Collections.reverseOrder());
16 System.out.println("-----------------------------------");
17 System.out.println("Sorting string array in descending or reverse order: ");
18 for(String string : strDesc)
19 {
20 System.out.println(string);
21 }
22 }
23}
1// java sort array of objects
2import java.util.Arrays;
3public class Employee implements Comparable<Employee>
4{
5 private String empName;
6 private int empAge;
7 public Employee(String name, int age)
8 {
9 this.empName = name;
10 this.empAge = age;
11 }
12 @Override
13 public String toString()
14 {
15 return "{" + "name='" + empName + '\'' + ", age=" + empAge + '}';
16 }
17 public String getName()
18 {
19 return empName;
20 }
21 public int getAge()
22 {
23 return empAge;
24 }
25 @Override
26 public int compareTo(Employee o)
27 {
28 if(this.empAge != o.getAge())
29 {
30 return this.empAge - o.getAge();
31 }
32 return this.empName.compareTo(o.getName());
33 }
34}
35public class SortArrayObjects
36{
37 public static void main(String[] args)
38 {
39 Employee[] obj = { new Employee("virat", 25), new Employee("dhoni", 20),
40 new Employee("rohit", 22), new Employee("rahul", 24)};
41 Arrays.sort(obj);
42 System.out.println(Arrays.toString(obj));
43 }
44}
1int[] array = new int[10];
2Random rand = new Random();
3for (int i = 0; i < array.length; i++)
4 array[i] = rand.nextInt(100) + 1;
5Arrays.sort(array);
6System.out.println(Arrays.toString(array));
7// in reverse order
8for (int i = array.length - 1; i >= 0; i--)
9 System.out.print(array[i] + " ");
10System.out.println();
11