Sounds like a good solution. You could do it with just an array of strings, of course.
Code:
const char *titles[] = {
"President",
"Vice-president",
/* ... */
};
Then you could just (linear) search in the titles[] array for the correct title, and the index in the array would be the "rank" of that title which you could use in a qsort() comparison function. For example (to determine rank):
Code:
for(size_t x = 0; x < sizeof(titles) / sizeof(*titles); x ++) {
if(!strcmp(this_title, titles[x])) return x;
}
(The sizeof() trick there just returns the number of elements in an array.)
But it may be more interesting to use structures -- by the way, you may be interested in this C syntax that allows you to initialize an array of structures . . .
Code:
struct title_t {
const char *name;
int rank;
} titles[] = {
{"President", 1},
{"Vice-president", 2},
/* ... */
};
The nice thing about doing this is that you could potentially have big gaps in the ranks (in case you use them for something else), or you could even do a binary search if you keep the names in sorted order. (Don't try to figure out a binary search out of your own head, by the way: it's nearly impossible . . . find some book or resource online to use as a basis.) Binary search is probably complete overkill in this situation of course, since it's much harder to code for little benefit on small-sized lists, but it's interesting to think about nevertheless . . . .
[Side note: I've used const char* pointers because string literals, strings that you type into the code between double quotes, are technically constant and are not supposed to be changed.]