C String Sorting...

BGM

Limp Gawd
Joined
Jul 6, 2001
Messages
456
Hey there,

I'm writing a little program in C, and have an array of structures.
typedef
struct
{
char fname[20];
char sname[20];
char address[40];
} book;

book record[200];
Id like to sort them alphabetically by using one element in each structure, (sname for example) but am not sure where to begin..

I was thinking of using the ASCII number of the first character of each string and doing an, if larger do such 'n' such, if smaller do such 'n' such.

At this point i became unstuck becasue i can't figure out a method to get the ASCII of the 1st character in the string! If anyone has an idea (and im sure you do!), or any other recommendations as to how to go about sorting my array of structures via a string held within them, it'd be great to hear.

Cheers :)
 
perhaps...

int ascii;

ascii = static_cast<int>(book[n].sname[0]);

then sort off ascii...

Not sure if that's right, been a while since I've done c
Similar would work in java though :)

-Idler
 
I'm assuming all the character arrays in the structure are strings. Why not just use a standard sorting algorithm using strcmp() to compair the strings.

KISS - Keep It Simple Stupid.
 
C sucks. Use C++ if you can.

Idler, static_cast is a C++ construct.
 
Back
Top