Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

c# strange collection issue

Posted on 2011-09-27
3
Medium Priority
?
206 Views
Last Modified: 2013-12-17
My old requirement was to maintain a list of 5 recently clicked product ids so that the last clicked was first in the list.

I did this by storing a semicolon delimited string, splitting it into an array, removing the number to be added from the array (if it existed), then adding it to the front of the list.  Creating a new array from a split function, then saving the proper number of elements.  This would ensure that if my new product was already clicked, it would move to the front.
so
5;3;6;1;7 and clicking on 7 would yelid 7;3;6;1

I just found out that I need to do the same thing, but maintain the list of 5 products per a list of 5 companines.
So
company1=5;3;6;1;7
company2=10
company3=21;22;28
clicking on company2 and product 13 would give me company2=10

If I already have 5 companines and I click on a new one, add it to the top, and get rid of the last.  

If the company already exists, add it to the beginning of the list, and add the product to the beginning of that list.
I have no idea how to tackle this.

Thanks
0
Comment
Question by:jackjohnson44
3 Comments
 
LVL 4

Accepted Solution

by:
theHollow earned 2000 total points
ID: 36712941
Hi
If I understand correctly, you can use a Dictionary.

Example:
Dictionary<String, List<Int32>> companydict;

that way, you can have the "top 5" list per key in the dictionary.
The key will be (in my example) a the company name. But you can change it to a custom made company class.

companydict = new Dictionary<String, List<Int32>>();
companydict.Add("Company #1", new List<Int32>() { 1, 6, 3, 7, 4 });
companydict.Add("Company #2", new List<Int32>() { 7, 34, 14, 43 });
companydict.Add("Company #3", new List<Int32>() { 77, 1 });

Open in new window

(the numbers are all examples).

You can then access the "top 5" list within each company name by doing like this:
companydict["Company #2"] will get you the list for "Company #2", which you can work with.

Example:
List<Int32> lst = companydict["Company #2"];
lst.Add(10);

...or directly:
companydict["Company #2"].Add(10);

But there are probably lots of good alternative ways of doing this.
I hope I have understood the problem correctly :-)
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 36713739
OK, I believe the answers are given in other posts.

"...you can use a Dictionary..."
Forgive me but this is an insane idea.
Dictionaries are very good collections but not in this case. You cannot guarantee an order in a Dictionary! Moreover, Dictionary has NO any idea about the order elements were added! It is a good idea to understand the underlying mechanism for hash-based collections (maps, dictionaries).
0
 

Author Comment

by:jackjohnson44
ID: 36718901
That is correct, a dictionary won't work because I need to maintain the order.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question