SOS!!! I need some help, I have 3 days :\
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
This is regular C, eh? Well, I was always a fan of
Then you can use
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.
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 --
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement