MrPickle5
MrPickle5

Reputation: 522

Sort these elements in descending order?

Experimenting with qsort and it runs perfectly for me. I use function pointers throughout the program and some other features I am not used to (i.e. such as void pointers).

I want the elements arranged in descending order (i.e. as opposed to ascending order), however. What can I do to achieve this?

Here is the code:

#include <iostream>
#include <cstdlib>  // Required for qsort
#include <cstring>
using std::cout;
using std::endl;

int compare_strs( const void *arg1, const void *arg2 );
int compare_ints( const void* arg1, const void* arg2 );

int main()
{
    char * shrooms[10] = 
    {
        "Matsutake", "Lobster", "Oyster", "King Boletus",
        "Shaggy Mane", "Morel", "Chanterelle", "Calf Brain",
        "Pig's Ear", "Chicken of the Woods"
    };

    int nums[10] = {99, 43, 23, 100, 66, 12, 0, 125, 76, 2};

    // The address of the array, number of elements
    // the size of each element, the function pointer to 
    // compare two of the elements
    qsort( (void *)shrooms, 10, sizeof( char * ), compare_strs ); 
    qsort( (void *)nums, 10, sizeof( int * ), compare_ints ); 

    // Output sorted lists
    for ( int i = 0; i < 10; ++i )
        cout << shrooms[i] << endl;

    for ( int i = 0; i < 10; ++i )
        cout << nums[i] << endl;

    return 0;
}

int compare_ints( const void * arg1, const void * arg2 )
{
    int return_value = 0;

    if ( *(int *)arg1 < *(int *)arg2 )
        return_value = -1;
    else if ( *(int *)arg1 > *(int *)arg2 )
        return_value = 1;

    return return_value;
}

int compare_strs( const void * arg1, const void * arg2 )
{
    return ( _stricmp( *(char **) arg1, *(char **) arg2 ) );
}

The program outputs in ascending order (i.e. starting with Calf Brain), but I am trying to get it to start with Shaggy Mane (i.e. descending order). Any help would be much appreciated.

Upvotes: 1

Views: 1612

Answers (3)

user93353
user93353

Reputation: 14049

Reverse the logic of your comparator functions.

inline int rcompare_strs( const void *arg1, const void *arg2 )
{
    return -1*compare_strs(arg1, arg2);
}

inline int rcompare_ints( const void* arg1, const void* arg2 )
{
    return -1*compare_ints(arg1, arg2);
}

qsort( (void *)shrooms, 10, sizeof( shrooms[0] ), rcompare_strs ); 
qsort( (void *)nums, 10, sizeof( nums[0] ), rcompare_ints ); 

Upvotes: 1

Ajay
Ajay

Reputation: 18441

Better use std::sort. There is no need to play around complicated qsort. Also, you should use std::string for storing strings, and std::vector to store them!

EDIT: Someone posted a commenet that std::sort won't magically reverse the sorting logic, so here is my reply:

And why not? std::sort algorithm takes a comparator also! Return negative-Boolean value, and you are done!

Upvotes: 2

Zeta
Zeta

Reputation: 105955

Use std::sort in conjunction with std::string and std::greater:

std::string shrooms[10] = 
{
    "Matsutake", "Lobster", "Oyster", "King Boletus",
    "Shaggy Mane", "Morel", "Chanterelle", "Calf Brain",
    "Pig's Ear", "Chicken of the Woods"
};

std::sort(shrooms, shrooms+10, std::greater<std::string>);

If you don't want to use std::sort simply inverse either the result of your comparison function or reverse your result.

Upvotes: 4

Related Questions