qsort function - trying to use a comparator

后端 未结 3 2033
执念已碎
执念已碎 2021-01-23 10:54

I made a qsort function for a larger program. It is to sort by time. I have a class schedule I am working with and found a need to compare time with AM and PM. ie if A is chosen

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-23 11:31

    Assuming you have a function greaterThan, you can implement sortFunction as follows:

    int sortFunction(const void *p, const void *q) {
        if (greaterThan(p, q)) { // p > q
            return +1;
        } else if (greaterThan(q, p)) { // p < q
            return -1;
        } else { // p == q
            return  0;
        }
    }
    

提交回复
热议问题