SOS!!! I need some help, I have 3 days :\

Started by
2 comments, last by Dietze 20 years, 9 months ago
If I have a structure to hold products. struct Products_{ char name[70]; //product name short ID; // ID of the product float price; // Price of the program }; and another to hold the source of the products, cause each source holds diferents products like: // Coke.product[03].ID; struct Source_{ struct Products_ Product[700]; // hold 700 products short ID; }; Source_ Source[7]; // now we have 7 sources (companies) and each one has 700 products NOW MY DOUBT:============================ How to sort by: - ID - NAME And how to use dates here? and sort by date? Please I need some answer, It''s my job in game :\ Rodrigo Dietze
Rodrigo Dietze
Advertisement
This is regular C, eh? Well, I was always a fan of qsort for sorting. First you create a comparison function:

int product_sorter_name_ascending(const void *v1, const void *v2){     Products_* p1 = (Products_*)v1;     Products_* p2 = (Products_*)v2;     // Change to strcmp for case-comparison     return stricmp(p1->name, p2->name);}


Then you can use qsort on each source:

for(int i = 0; i < 7; i++){   qsort(Source[i].Product,700,sizeof(Products_),product_sorter_name_ascending);}


And that''s the gist of it. You can modify your comparison function for descending order, or change it to check ID''s as well. I don''t see dates anywhere so I don''t know what format those are in.
Well you could consider using STL, or you can just google search and use a tried-and-true search algorithm.

-- Steve --
-- Steve --
Oh!! Some good souls!!

Thanks a lot!!

I''m very grateful

I could lose my job if I didn''t that. :\

Thanks

Rodrigo Dietze
Rodrigo Dietze

This topic is closed to new replies.

Advertisement