简体   繁体   中英

How to sort an array of objects using MergeSort in java without comparator

I need to sort a huge array of objects. Each object contains a number. I need to sort it by that number, but I can't use a comparator. Is it possible to do it using a mergesort algorithm?

As long as each object has a way to be compared (typically done in a 'compareTo(T other)' function), mergesort is a viable solution.

EDIT: More thoroughly, it would look something like this:

public int compareTo(T other){
    if this.getNumber() == other.getNumber(){
        return 0;
    }
    else if this.getNumber() > other.getNumber(){
        return 1;
    }
    else{
        return -1;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM