Solved

c# strange collection issue

Posted on 2011-09-27
3
199 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 500 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 29

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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