By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,195 Members | 618 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,195 IT Pros & Developers. It's quick & easy.

Sorting by Alphabetical order using strings in data structures

P: 22
Here is my problem:

Structure
typedef struct
{
char lname[25];
char fname[25];
int age;
double salary;
} employee_t;

I have read data in from a binary file (example):
Doe John 21 40000
(lastname) (firstname) (age) (salary)


PROBLEM

I am passing the structure to a function which sorts the last names in Alphabetical order.

void sort(employee_t*, int)
employee_t = the structure int = number of employees read in.

I need this sort function to sort those employees in alphabetical order.

If anyone can help me out by getting me started or helping me along that would be fantastic. I know I need to essentially use 2 "for" loops using a counters, and strcmp() some where within the loops. Some one help me please!
Apr 28 '07 #1
Share this Question
Share on Google+
7 Replies

Ganon11
Expert 2.5K+
P: 3,652
Which sorting algorithm will you be using? For simplicity, I'd suggest the Selection Sort; for speed, I'd suggest the Quick Sort.

You say you are sorting the employees alphabetically...by first name? By last name? By some other variable?
Apr 28 '07 #2

P: 29
Hold on, I'm writing you a function now...
Apr 28 '07 #3

P: 29
Here's some sample code that should solve your problem. Right now it's sorting (Bubble Sort) by last name, but you can easily change that. I hope this helps!

<Code removed

MODERATOR>
Apr 28 '07 #4

Savage
Expert 100+
P: 1,764
Hold on, I'm writing you a function now...
And,u hold too hold on,first read posting guidelines.If u wish to help OP just write him a pseudocode and don't write him whole function!

I suggest to remove it before a moderator those.


Savage
Apr 28 '07 #5

P: 29
And,u hold too hold on,first read posting guidelines.If u wish to help OP just write him a pseudocode and don't write him whole function!

I suggest to remove it before a moderator those.


Savage
Oops, didn't read them. You're right though, but how do I delete it?
Apr 28 '07 #6

Savage
Expert 100+
P: 1,764
Oops, didn't read them. You're right though, but how do I delete it?
Now u can't delete it,u can delete it only 5 minutes after u posted it.Only moderator/admin can delete it now.

Savage
Apr 29 '07 #7

Ganon11
Expert 2.5K+
P: 3,652
Oops, didn't read them. You're right though, but how do I delete it?
I have taken care of it. Make sure you read through the Posting Guidelines when you get a chance ;)
Apr 30 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.