How to sort an ArrayList

All I need is the easiest ArrayList sort method that doesn't use the built-in Java sorter. I am currently changing my ArrayList to an array and using the liner sort code, but later I need to call some elements, and ArrayLists are easier to do.

+3
source share
9 answers

you can use anonymous sorting.

Collections.sort(<ArrayList name>, Comparator<T>() {

    public int compare(T o1, T o2) {
    .....
    ....
    }      
});

where T is the type you want to sort (e.g. String, Objects) and just implement the Comparator interface for your needs

+5
source

Assuming ArrayList<String> a...

The simplest (but I assume that this is what you say you cannot use):

Collections.sort(a);

The following is the easiest (but empty):

a = new ArrayList<String>(new TreeSet<String>(a));
+3
source

, " " Collections.sort(), , , ArrayList

ArrayList list = new ArrayList(Arrays.asList(sortedArray));

(, ArrayList) get(int index) set(int index, E element).

+3

, ; Array.sort

public class Sort {

    public static void main(String args[]) 
    {
        for(int j = 0; j < args.length; j++) 
        {
            for(int i = j + 1; i < args.length; i++) 
            {
                if(args[i].compareTo(args[j]) < 0) 
                {
                    String t = args[j];
                    args[j] = args[i];
                    args[i] = t;
                }
            }
            System.out.println(args[j]);
        }
    }
}

Array.sort

import java.util.*;
public class IntegerArray {

    public static void main(String args[])
    {
        int[] num=new int[]{10, 15, 20, 25, 12, 14};
    Arrays.sort(num);
        System.out.println("Ascending order: ");
        for (int i=0; i<num.length; i++)
            System.out.print(num[i] + " ");
        }
}
+2

Comparator java. Collections.sort(..) arraylist, Comparator

0

, arrayList, . , , arrayList. - . , . , 10 10 , . . 8 .

0

, . , , , .

, , . , , , . .

, :

  • , .
  • , Comparable, Comparator. javadocs. ( - ...)
0

"" :

public Comparable<Object>[] quickSort(Comparable<Object>[] array) {
    if (array.length <= 1) {
        return array;
    }

    List<Comparable<Object>> less = new ArrayList<Comparable<Object>>();
    List<Comparable<Object>> greater = new ArrayList<Comparable<Object>>();
    Comparable<Object> pivot = array[array.length / 2];

    for (int i = 0;i < array.length;i++) {
        if (array[i].equals(pivot)) {
            continue;
        }
        if (array[i].compareTo(pivot) <= 0) {
            less.add(array[i]);
        } else {
            greater.add(array[i]);
        }
    }

    List<Comparable<Object>> result = new ArrayList<Comparable<Object>>(array.length);
    result.addAll(Arrays.asList(quickSort(less.toArray(new Comparable<Object>[less.size()]))));
    result.add(pivot);
    result.addAll(Arrays.asList(quickSort(greater.toArray(new Comparable<Object>[greater.size()]))));
    return result.toArray(new Comparable<Object>[result.size()]);
}

Recent operations with arrays and a list to build the result can be expanded with System.arraycopy.

0
source

All Articles