---
# System prepended metadata

title: PID-1042

---

To sort an ArrayList in Java without using the `sort()` method and without converting it into an array, we can use a simple sorting algorithm such as bubble sort or selection sort. Here's an example of sorting an ArrayList of integers using bubble sort:

```java
import java.util.ArrayList;

public class SortArrayList {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(5);
        list.add(2);
        list.add(8);
        list.add(3);
        list.add(1);
        list.add(4);

        int n = list.size();
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < n-i-1; j++) {
                if (list.get(j) > list.get(j+1)) {
                    int temp = list.get(j);
                    list.set(j, list.get(j+1));
                    list.set(j+1, temp);
                }
            }
        }

        System.out.println(""Sorted ArrayList: "" + list);
    }
}
```

This program first initializes an ArrayList of integers with some values. It then sorts the ArrayList using bubble sort and prints the sorted values. The outer loop runs from 0 to n-1, where n is the size of the ArrayList. The inner loop runs from 0 to n-i-1, where i is the index of the outer loop. In each iteration of the inner loop, the adjacent elements are compared and swapped if they are in the wrong order. After each iteration of the outer loop, the largest element is moved to the end of the ArrayList. This process is repeated until the ArrayList is sorted in ascending order [1][5].

Note that bubble sort is not the most efficient sorting algorithm, especially for large ArrayLists. Other sorting algorithms such as quicksort or mergesort may be more appropriate for larger datasets.