1// how to sort an array in java without using sort() method (ascending order)
2public class WithoutSortMethod
3{
4 public static void main(String[] args)
5 {
6 int temp;
7 int[] arrNumbers = {14, 8, 5, 54, 41, 10, 1, 500};
8 System.out.println("Before sort: ");
9 for(int num : arrNumbers)
10 {
11 System.out.println(num);
12 }
13 for(int a = 0; a < arrNumbers.length; a++)
14 {
15 for(int b = a + 1; b < arrNumbers.length; b++)
16 {
17 if(arrNumbers[a] > arrNumbers[b])
18 {
19 temp = arrNumbers[a];
20 arrNumbers[a] = arrNumbers[b];
21 arrNumbers[b] = temp;
22 }
23 }
24 }
25 System.out.println("---------------");
26 System.out.println("After sort: ");
27 for(int num : arrNumbers)
28 {
29 System.out.println(num);
30 }
31 }
32}
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}
1// how to sort an array in java without using sort method
2public class WithoutUsingSortMethod
3{
4 public static void main(String[] args)
5 {
6 int temp;
7 int[] arrNumbers = {14, 8, 5, 54, 41, 10, 1, 500};
8 System.out.println("Before sort: ");
9 for(int num : arrNumbers)
10 {
11 System.out.println(num);
12 }
13 for(int a = 0; a <= arrNumbers.length - 1; a++)
14 {
15 for(int b = 0; b <= arrNumbers.length - 2; b++)
16 {
17 if(arrNumbers[b] < arrNumbers[b + 1])
18 {
19 temp = arrNumbers[b];
20 arrNumbers[b] = arrNumbers[b + 1];
21 arrNumbers[b + 1] = temp;
22 }
23 }
24 }
25 System.out.println("---------------");
26 System.out.println("After sort: ");
27 for(int num : arrNumbers)
28 {
29 System.out.println(num);
30 }
31 }
32}
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 program to sort strings in an alphabetical order
2import java.util.Arrays;
3public class SortStringAlphabeticalOrder
4{
5 public static void main(String[] args)
6 {
7 String[] strAsc = {"Bear","Fox","Deer","Cheetah","Anteater","Elephant"};
8 System.out.println("Before sorting: ");
9 for(String string : strAsc)
10 {
11 System.out.println(string);
12 }
13 // arrays.sort
14 Arrays.sort(strAsc);
15 System.out.println("-----------------------------------");
16 System.out.println("After sorting: ");
17 for(String str : strAsc)
18 {
19 System.out.println(str);
20 }
21 }
22}
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}