[ back to toc ]

Qsort()

Date: 2002/03/14 13:25

Q:
Hello Peter,
I am doing a complicated program as an exercise as part of a long-distance
course.It involves reading a variable-length text-file-could be 20-200
records),and sorting it with qsort(),malloc() etc.Unfortunately, the
text-file is made up of a continuous list of 3 different records,each of
differing length,and each in random order in the file.I had no trouble in
validating them,but trouble is,qsort() can only compare 2 different
structures/unions,and I believe the variable must be the same length in
each case.By the way, the 3 record/structures,which I put into an overall
union fro practicality, have 2 common fields - the first character of each
of the 3 records is a record-type,a letter of the alphabet;the next 5
digits of each record is a customer code number, which I have been told to
sort into order with qsort(). Please help me, as I am not allowed to use
anything other than qsort() and I am at my wit's end!
Yours Grateful For Any Help You Can Give Me!
*NAME-DELETED* *NAME-DELETED*
A:
*NAME-DELETED*,

qsort can compare anything that you provide a comparision function for.

Store the records in memory and have an array of pointers to the records.
Compare in your comparision function the records based on the pointers and
sort only the pointers, which indeed have fixed length.

That is it.

Peter

[ back to toc ]