Solved

c# strange collection issue

Posted on 2011-09-27
3
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

630 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