Solved

c# strange collection issue

Posted on 2011-09-27
3
200 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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