hcps-tenembasj
hcps-tenembasj

Reputation: 343

Writing a sorting method for any Collection of Comparables (Java)

This is my first post on Stack Overflow. If there is anything I can do to improve it: please share.

I am trying to learn about different sorting algorithms (such as insertion, selection, bubble, merge, quick, bucket, radix, heap, shell, gnome, and bogo sort) and implement them in Java.

How could I write a sort method which will work with any class that implements Collection of any class that implements Comparable? I looked at the source of Collections.sort() and it has some syntax in the method signature I don't understand.

public static <T extends Comparable<? super T>> void sort(List<T> list) {
    Object[] a = list.toArray();
    Arrays.sort(a);
    ListIterator<T> i = list.listIterator();
    for (int j=0; j<a.length; j++) {
        i.next();
        i.set((T)a[j]);
    }
}

The following is my best (pathetic) guess of how to write a SortNone method, which doesn't sort a Collection, but has the correct method header to do so. I would call it with SortNone.sort(myCollection) where myCollection is an instance of any class implementing Collection of a Class implementing Comparable (assuming the prior import of com.tenembasj.sort.*, see More Info).

package com.tenembasj.sort;

public class SortNone {

    public static<T> void sort(Collection<E> collection) { //don't know how to require the object (is it T or E?) to implement Comparable
        //code to sort
        //would I have to use collection.size(), .get(i), .set(i), and such to edit the class implementing Collection?
    }
}

Furthermore, I would like to add a sort method which adds a parameter for a Comparator, I just need to know what the formal parameter would be. I think I can figure out the implementation once I know how to write the regular sort.

Again, I am trying to write my own sorting methods and not use Collections.sort(), Arrays.sort(), or any Java.util method. You might say "Don't reinvent the wheel", but I'm just trying to learn.

More Info

I would like to export these methods to a jar when I'm done and import them as a library in my future projects. I made a new project (in Eclipse), created a package called com.tenembasj.sort, and will add classes such as MergeSort. I believe this is standard naming convention, but if it isn't please tell me. My goal is to be able to type import com.tenembasj.sort.* in any of my future projects and call MergeSort.sort(myCollection); or MergeSort.sort(myCollection, myComparator);.

Upvotes: 1

Views: 2368

Answers (1)

Dileep
Dileep

Reputation: 5440

How could I write a sort method which will work with any class that implements Collection of any class that implements Comparable?

For writing a function that can be applied to all type of objects, You must write a generic code like the code that you posted above from the source of Collections.sort().

Assuming that you are a beginner, the best place to start is to learn from generic and eat the elephant one bite at a time.


I will try to explain the code you have posted above.

T means of any type in generic

extends Comparable<? super T> means that T must be a Comparable object. It can be of type Object that extends a Comparable interface or any object that contain a comparable interface.

sort(List<T> list) Means sort function can accept any List of objects of type T as argument.

public static <T extends Comparable<? super T>> void sort(List<T> list) {

//  Copying the values from the List of objects to an array.
    Object[] a = list.toArray();

//  Applying the array.sort operation.
    Arrays.sort(a);

//  Setting the values from the sorted array to the listi of type T. 
    ListIterator<T> i = list.listIterator();
    for (int j=0; j<a.length; j++) {
        i.next();
        i.set((T)a[j]);
    }
}

Start by learning the generic

Upvotes: 1

Related Questions