This is the mail archive of the newlib@sources.redhat.com mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: BUG: qsort reorders elements in already sorted array


On Wed, Jun 04, 2003 at 01:49:18PM -0400, James Roth wrote:
>    It sounds like you want to sort by more than one key.  Why not have 
> your compare function actually do that.  This way, the undefined qsort 
> results are not relevant.
> 
> struct info
> {
>    int score;
>    int date; /* or article number, etc. */
> };
> 
> int
> compare (void *v1, void *v2)
> {
>    struct info *i1 = v1, *i2 = v2;
> 
>    if (v1->score - v2->score)  /* Maybe you want v2 - v1 */
>        return v1->score - v2->score; /* Scores are different, so ignore 
> date. */
>   else
>        return v1->date - v2->date; /* Scores are same, so use date. */
> }

That seems to be a good idea to suggest to the tin ML, I guess.

Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Developer
Red Hat, Inc.
mailto:vinschen@redhat.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]