Search code examples
javaarraylistselectioncomparatorselection-sort

How to use selection sort with comparator in java?


help here will be greatly appreciated. I'm pretty stuck. What I have to do is I have to use the selection sort algorithm to sort an arraylist, but there are multiple indexes in each object in the arraylist. This has to be done in java.

For example:

public class a {

    private int number;
    private String letter;

    public a(int n, String l)
    {
        number = n;
        letter = l;
    }
}

public class SortingArrays {

    private ArrayList<a> myarray;

    private Comparator<a> sortByNumber;
    private Comparator<a> sortByLetter;

    public FootballPlayerData() {

        myarray = new ArrayList<a>();

        getmyarray().add(new a(2, "A"));
        getmyarray().add(new a(7, "J"));

        //COMPARATORs//
        sortByNumber = new Comparator<a>() {
            @Override
            public int compare(a o1, a o2) 
            {
                if (o1.number < (o2.number)) {
                    return -1;
                }
                if (o1.number == (o2.number)) {
                    return 0;
                }
                return 1;
            }

        };
        sortByLetter = new Comparator<a>() {
            @Override
            public int compare(a o1, a o2) 
            {
                return o1.letter.compareTo(o2.letter);
            }

         };

    public void selectionSortbyNumber
    {
        ???
    }
    public void selectionSortbyLetter
    {
        ???
    }
}

So how do I create a selection sort in java (has to be selection sort) that sorts the arraylist by different elements within the objects? I already have the comparator part down, but I don't know how to incorporate that with selection sort.


Solution

  • A Comparator implementation is usually used to compare two elements with one another, returning -1 (or any negative number) if the first element is less than the second, 0 if they are equal, and 1 (or any positive number) if the first element is greater than the second. This can be used to compare two elements to see if one is greater, less than, or equal to the other.

    In the context of selection sort, you can use a supplied comparator to determine which value in the unsorted portion of the list is the minimum. The general algorithm for selection sort is as follows:

    for i from 0 to array.length:
        current_minimum_index = i
        for j from i + 1 to array.length:
            if array at j is less than array at current_minimum_index:
                current_minimum_index = j
    
            swap array at current_minimum_index with array at i
    

    The if array at j is less than array at current_minimum_index can be implemented using Comparator. For example, given a supplied ArrayList called array, the call to the Comparator object named comparator would be:

    if (comparator.compare(array.get(j), array.get(current_minimum_index))) < 0)
    

    I do not want to provide you with the complete answer, as that would not help you learn selection sorting, but the method signature for your sorting would resemble the following:

    public <T> void selectionSort(ArrayList<T> array, Comparator<T> comparator) {
    
        // for i from 0 to array.size():
            // currentMinIndex = i
            // for j from i + 1 to array.size():
                if (comparator.compare(array.get(j), array.get(currentMinIndex))) < 0) {
                    // currentMinIndex = j
                }
    
                // swap array at currentMinIndex with array at i
    }
    

    Your call to this method would then look like one of the following:

    selectionSort(myarray, sortByNumber);
    selectionSort(myarray, sortByLetter);