Note that there are some explanatory texts on larger screens.

plurals
  1. POSort these elements in descending order?
    primarykey
    data
    text
    <p>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).</p> <p>I want the elements arranged in descending order (i.e. as opposed to ascending order), however. What can I do to achieve this?</p> <p>Here is the code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;cstdlib&gt; // Required for qsort #include &lt;cstring&gt; 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 &lt; 10; ++i ) cout &lt;&lt; shrooms[i] &lt;&lt; endl; for ( int i = 0; i &lt; 10; ++i ) cout &lt;&lt; nums[i] &lt;&lt; endl; return 0; } int compare_ints( const void * arg1, const void * arg2 ) { int return_value = 0; if ( *(int *)arg1 &lt; *(int *)arg2 ) return_value = -1; else if ( *(int *)arg1 &gt; *(int *)arg2 ) return_value = 1; return return_value; } int compare_strs( const void * arg1, const void * arg2 ) { return ( _stricmp( *(char **) arg1, *(char **) arg2 ) ); } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload