[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
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"));
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
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
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++;
}
}
Re: [RESOLVED] Sorting a Generic List
Quote:
Originally Posted by
bixel
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