How to sort array of strings in descending order C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static int compare_function(const void* a, const void* b) {
    return strcmp(*(const char**)b, *(const char**)a);
}

int main(void)
{
    char* array[] = {"java",
                     "c++",
                     "c#",
                     "go",
                     "python",
                     "c" };

    int size = sizeof(array) / sizeof(array[0]);

    qsort(array, size, sizeof(const char*), compare_function);

    for (int i = 0; i < size; i++)
        puts(array[i]);

    return 0;
}




/*
run:

python
java
go
c++
c#
c

*/

 



answered Dec 31, 2023 by avibootz

Related questions

1 answer 144 views
1 answer 105 views
2 answers 275 views
1 answer 296 views
...