Archived

This topic is now archived and is closed to further replies.

Dietze

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

Recommended Posts

Dietze    122
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

Share this post


Link to post
Share on other sites
Zipster    2359
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.

Share this post


Link to post
Share on other sites
spg    122
Well you could consider using STL, or you can just google search and use a tried-and-true search algorithm.

-- Steve --

Share this post


Link to post
Share on other sites