i created a list but i want to sort it with a custom IComparer… the IComparer class is this
public class Comp : IComparer<int>
{
public int Compare (int x, int y)
{
if (x == null) {
if (y == null) {
return 0;
} else {
return 1;
}
} else {
if (y == null) {
return -1;
} else {
int difference = x.CompareTo (y);
if (difference != 0) {
return difference;
} else {
return 0;
}
}
}
}
}
Now in the same script I call a function that Add objects in the list, but at the end I want sort the list
Comp comparer = new Comp ();
foreach (GameObject item in items)
{
int i = Array.IndexOf (items, item);
ItemData itemScript = item.GetComponent <ItemData> ();
yield return itemScript.itemState;
itemSt.Add (new ItemStat (itemScript.ID, itemScript.itemState, item.name));
if (items <em>.name == itemSt _.Name && itemSt *.State == "Destroyed")*_</em>
* {*
* Destroy (item.gameObject, 0.0F);*
* }*
* }*
* itemSt.Sort (comparer);*
But the itemSt.Sort (comparer); line give two errors:
The best overloaded method match for*_</em> <em>_*System.Collections.Generic.List<ItemStat>.Sort(System.Collections.Generic.IComparer<ItemStat>) has some invalid arguments
Argument #1' cannot convert Comp' expression to type System.Collections.Generic.IComparer<ItemStat>'
I don’t understand the problem… why i can’t sort this list that contains variables from ItemStat class?