Solved

Ordering a List Based on Another List of Values

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now