Solved

Ordering a List Based on Another List of Values

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tool Box 2 35
Not showing JavaScript in the list 5 40
Server Error 11 48
How to join on ID, with prefix? 15 0
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

20 Experts available now in Live!

Get 1:1 Help Now