Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Message-ID | <4EBDCB7A.2B15@mindspring.com> (permalink) |
|---|---|
| Date | 2011-11-11 20:27 -0500 |
| From | pete <pfiland@mindspring.com> |
| Organization | PF |
| Newsgroups | comp.lang.c |
| Subject | Re: qsort |
| References | <4ebd2a87$0$19698$bbae4d71@news.suddenlink.net> |
Bill Cunningham wrote:
> double prices[]={24.45,24,21};
>
> I want to sort them from highest to lowest.
> Still not knowing C's semantics
> well enough there's probably a simpler way
> but what kind of function would I
> want to write to pass to that last parameter of qsort?
/* BEGIN output from new.c */
From highest to lowest:
24.450000
24.000000
21.000000
/* END output from new.c */
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
int
price_comp(const void *a, const void *b)
{
const double *aa = a;
const double *bb = b;
return *aa > *bb ? -1 : *aa != *bb;
}
int
main(void)
{
double prices[] = {24.45, 24, 21};
double *ptr = prices;
size_t n = sizeof prices / sizeof *prices;
qsort(prices, n, sizeof *prices, price_comp);
puts("\n/* BEGIN output from new.c */\n");
puts("From highest to lowest:");
while (n-- != 0) {
printf("%f\n", *ptr);
++ptr;
}
puts("\n/* END output from new.c */");
return 0;
}
/* END new.c */
--
pete
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-11 09:00 -0500
Re: qsort Malcolm McLean <malcolm.mclean5@btinternet.com> - 2011-11-11 06:56 -0800
Re: qsort Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-11 16:39 +0000
Re: qsort 88888 Dihedral <dihedral88888@googlemail.com> - 2011-11-12 10:19 -0800
Re: qsort John Gordon <gordon@panix.com> - 2011-11-11 15:20 +0000
Re: qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-11 14:22 -0500
Re: qsort pete <pfiland@mindspring.com> - 2011-11-11 20:27 -0500
Re: qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-12 19:10 -0500
Re: qsort Keith Thompson <kst-u@mib.org> - 2011-11-12 16:40 -0800
Re: qsort "Bill Cunningham" <nospam@nspam.invalid> - 2011-11-13 01:39 -0500
Re: qsort Ian Collins <ian-news@hotmail.com> - 2011-11-13 19:51 +1300
Re: qsort Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-11-14 02:14 +0200
Re: qsort Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-14 00:43 -0800
Re: qsort pete <pfiland@mindspring.com> - 2011-11-12 22:42 -0500
csiph-web