CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2008
    Posts
    154

    [RESOLVED] Sorting a Generic List

    1st > Not using Forms!

    so I have a class called Label that draws text on a screen - and no this is not Windows Forms!

    I have a Party and that Party has an inventory, like items, weapons and stuff. I have a GUI screen where the inventory is displayed like so

    Code:
    Weapons         Equipped / Count
    Bow                          1/2
    Dagger                      1/1
    Mace                        0/3
    Sword                       2/4
    so on the far left is a List<Labels> and on the far right is another List<Labels>
    I gave the Label class a property called name, that way Label Bow, Bow.Name = Bow
    since the other Label 1/2 needs to be paired with Label Bow, that Label.Name also = Bow

    Here is the problem, depending on how the Labels are added to the List, they may be displayed improperly, especially on the Labels that are just a bunch of numbers like 2/4, so I need to Sort them alphabetically by their Label.Name inside the List<Label>
    and... I don't know how to do that

    I tried to use

    Code:
    Sort(Comparison<T> comparison);
    but I don't know how that works

  2. #2
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Sorting a Generic List

    What I usually do is create a Comparer like so:

    Code:
    class LabelComparer : IComparer<Label>
    {
        private string field;
    
        public LabelComparer(string field)
        {
            this.field = field;
        }
    
        public int Compare(Label a, Label b)
        {
            int result = 0;
            switch (field)
            {
                case "Name":
                        result = a.Name.CompareTo(b.Name);
                    break;
            }
        }
    }
    And then do a Sort like this

    Code:
    labels.Sort(new LabelComparer("Name"));
    It's not a bug, it's a feature!

  3. #3
    Join Date
    Jun 2008
    Posts
    154

    Re: Sorting a Generic List

    hrmm... getting a

    does not implement interface member & not all code paths return a value

    labels.Sort(new LabelComparer("Name"));

    so its saying, Sort a LabelComparer with the field of "Name"

    I was tying to use this >>> http://blog.codewrench.net/2009/04/1...rary-property/

    but he has too many compile errors in the code

    Thanks for the effort tho! - I will continue tweaking it

  4. #4
    Join Date
    Jun 2008
    Posts
    154

    Re: Sorting a Generic List

    ok picked up on a couple of examples but so far no success

    1st > in Label
    Code:
            public static Comparison<Label> NameComparison = delegate(Label p1, Label p2)
            {
                return p1.Name.CompareTo(p2.Name);
            };
    
            public int CompareTo(Label other)
            {
                return Name.CompareTo(other.Name);
            }
    then using it

    labels.Sort(Label.NameComparison);

    does nothing..

    got this idea from > http://dotnetslackers.com/Community/...0_T_3E00_.aspx

  5. #5
    Join Date
    Jun 2008
    Posts
    154

    Re: [RESOLVED] Sorting a Generic List

    Ok!!!! I solved it - its a hack really, the List<Labels> is getting populated from a Dictionary, and since a Dictionary cannot be sorted unless its recreated I simply sorted it BEFORE I created the list

    Code:
            private void RefreshInvWeaponListLabels()
            {
                List<string> weaponNames = new List<string>();
                foreach (KeyValuePair<string, Fruple<WeaponType, int, int, string>> weapon in GameRef.AdventureParty.InvWeapons)
                {
                    string invWeapon = weapon.Key;
                    weaponNames.Add(invWeapon);
                }
                weaponNames.Sort();
    
                int i = 0;
                foreach (string name in weaponNames)
                {
                    int weaponCount = GameRef.AdventureParty.InvWeapons[name].Third;
                    int weaponEquipCount = GameRef.AdventureParty.InvWeapons[name].Second;
                    string weaponLoadOut = Convert.ToString(weaponEquipCount) + "/" + Convert.ToString(weaponCount);
                    invWeaponCount[i].Text = weaponLoadOut;
                    i++;
                } 
            }

  6. #6
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: [RESOLVED] Sorting a Generic List

    Quote Originally Posted by bixel View Post
    the List<Labels> is getting populated from a Dictionary, and since a Dictionary cannot be sorted unless its recreated I simply sorted it BEFORE I created the list
    then maybe you should try the SortedDictionary<TKey, TValue> Class
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured