Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Ordering a List Based on Another List of Values

Posted on 2011-02-12
3
Medium Priority
?
354 Views
Last Modified: 2013-12-17
I have a class called Inventory, with a list of Locations that have a key of what order I want them to be sorted on. I was ASSuming a SortedList would keep the values in order by the keys, but it does not.  Below is the class and the console appplication I'm trying to test it with.
object1 should be sorted => FWBB3B,A2M4,EWM4. The list is thrown back in the same order that it is added.

public class Inventory
    {
        public string InvNumber { get; set; }
        public int Quantity { get; set; }
        public string Description { get; set; }
                
        SortedList<int,string> Location = new SortedList<int, string>();
        SortedList<int,string> LocationList = new SortedList<int,string>()
        {
             {1,"C5"},{2,"FW"},{3,"A1"},{4,"A2"},{5,"B1"},{6,"B2"},{7,"EW"},{8,"C1"},{9,"C2"},{10,"C3"},{11,"C4"},{12,"Howie"},{13,"MA"},
             {14,"D1"},{15,"D2"},{16,"D3"},{17,"D4"},{18,"D5"},{19,"E1"},{20,"E2"},{21,"F1"},{22,"F2"},{23,"GB"},{24,"G1"},{25,"G2"},
             {26,"BW"},{27,"G3"},{28,"G4"},{29,"H1"},{30,"H2"},{31,"H3"},{32,"H4"},{33,"I1"},{34,"I2"},{35,"J1"},{36,"J2"},{37,"K1"},
             {38,"K2"},{39,"L1"},{40,"L2"},{41,"L3"},{42,"L4"},{43,"M1"},{44,"M2"},{45,"M3"},{46,"M4"},{47,"N1"},{48,"N2"},{49,"N3"},
             {50,"N4"},{51,"NW"},{52,"O1"},{53,"O2"},{54,"O3"},{55,"O4"},{56,"SW"},{57,"BRP"}
        };

        public Inventory()
        {
            this.InvNumber = "";
            this.Quantity = 0;
            this.Description = "";
        }

        public Inventory(string invNumValue, string quantityValue, string locationValue, string deascriptionValue)
        {
            this.InvNumber = invNumValue;
            this.Quantity = Int16.Parse(quantityValue);
            BuildLocationList(locationValue);
            this.Description = deascriptionValue;

        }

        private void BuildLocationList(string locationValue)
        {
            string[] templocations = locationValue.Split(',');
            //SortedDictionary<int,string> tempdict = new SortedDictionary<int, string>();

            foreach (string s in templocations)
            {
                foreach (KeyValuePair<int, string> kvp in LocationList)
                {
                    if (s.Trim().StartsWith(kvp.Value))
                    {
                        Location.Add(kvp.Key, s.Trim());
                    }
                }

            }
            //Location.Sort(delegate(int x, int y) { return x.CompareTo(y); });

         }

     }

Open in new window

class Program
    {
        static void Main(string[] args)
        {
            Inventory object1 = new Inventory("80401-W","1","EWM4,A2M4,FWBB3B","AJDHJKAHSDJKHASDJKH");
            Inventory object2 = new Inventory("80401-E", "10", "B1K4,O2PP3B,A2J3", "AJDHJKAHSDJKHASDJKH");

            foreach (string s in object1.Location)
            {
                Console.WriteLine(s);
            }
            

            Console.ReadLine();

            
        }
    }

Open in new window

0
Comment
Question by:frtools
[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
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 34879624
It's because you are pulling them out as strings, not key/value pairs. Mine worked great when I changed the code in program to
      foreach (KeyValuePair<int, string> s in object1.Location)
      {
        Console.WriteLine(s.Value);
      }

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 34879646
I also had to make Location public to get it to compile
0
 

Author Closing Comment

by:frtools
ID: 34879860
Perfect, never thought that the testing application was feeding back wrong information.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 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