[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Ordering a List Based on Another List of Values

Posted on 2011-02-12
3
Medium Priority
?
355 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
  • 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

830 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