sort struct 
Author Message
 sort struct

Hi,
Can someone point me in the right direction to sort a struct:
It contains 2 members, the first a name and the second a number. I want
to sort alphabetically. If any knows of some man pages or something I
can look at, it would be great.



Fri, 25 Aug 2000 03:00:00 GMT  
 sort struct

Quote:

>Hi,
>Can someone point me in the right direction to sort a struct:
>It contains 2 members, the first a name and the second a number. I want
>to sort alphabetically. If any knows of some man pages or something I
>can look at, it would be great.

Make a comparison function that examines the name first [assuming it is the
most significant item of the sort]. If and only if they are equal, compare the
number.  See your man page for qsort() as prototyped in stdlib.h
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com


Fri, 25 Aug 2000 03:00:00 GMT  
 sort struct

: Hi,
: Can someone point me in the right direction to sort a struct:
: It contains 2 members, the first a name and the second a number. I want
: to sort alphabetically. If any knows of some man pages or something I
: can look at, it would be great.

The easiest way (assuming that you're sorting an array of structures)
would be to call the qsort() function; you can look it up in your
compiler docs.  Otherwise, you'll have to write your own sort routine.
Usually, you compare strings with strcmp().

Will



Sat, 26 Aug 2000 03:00:00 GMT  
 sort struct


Quote:

>Hi,
>Can someone point me in the right direction to sort a struct:
>It contains 2 members, the first a name and the second a number. I want
>to sort alphabetically. If any knows of some man pages or something I
>can look at, it would be great.

Make a comparison function, then use qsort().

BTW, you should read the clc FAQ.

Aloha



Sat, 26 Aug 2000 03:00:00 GMT  
 sort struct

On Mon, 09 Mar 1998 21:04:05 -0800, Brian Charles

Quote:

>Can someone point me in the right direction to sort a struct:
>It contains 2 members, the first a name and the second a number. I want
>to sort alphabetically. If any knows of some man pages or something I
>can look at, it would be great.

Hello Brian.  This is an excellent question.  The problem is a common
one among C programmers, and so the solution has been added to the
C-faq.  I always recommend reading the C-faq in its entirety to all C
programmers, since it provides many tips for avoiding pitfalls and
solutions to commonly faced problems.  However, you should find
question 13.9 particularly relevant to your problem.

  http://www.eskimo.com/~scs/C-faq/top.html

"Read the C-faq.  FOR EXTERNAL USE ONLY.  IF TAKEN INTERNALLY, SERIOUS
GASTRIC DISTURBANCES MAY RESULT."

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Sat, 26 Aug 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Sorting structs

2. How to sort structs by particular member?

3. Q: Sorting structs

4. sorting structs with qsort()??

5. sorting structs

6. struct member sort

7. help me sort my array of structs

8. Sort array of struct

9. sorting an array of structs

10. struct member sort

11. struct sort

12. Question About Sorting an Array of Structs

 

 
Powered by phpBB® Forum Software