Solved

Ordering a List Based on Another List of Values

Posted on 2011-02-12
3
349 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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 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