Search Unity

Sorting a list with class.name alphabetically

Discussion in 'Scripting' started by Dawnreaver, Oct 12, 2013.

  1. Dawnreaver

    Dawnreaver

    Joined:
    Oct 13, 2010
    Posts:
    131
    Hey Folks ^_^

    So I'm sort of working on an inventory system. Right now I storing the items in a list.

    The problem is: I would like to sort the list by the name of the item. I can access the name of the item by:

    Code (csharp):
    1. item.itemName
    Using the sort function however is throwing an exception:

    Code (csharp):
    1. ArgumentException: does not implement right interface
    2. System.Collections.Generic.Comparer`1+DefaultComparer[ItemClass].Compare (.ItemClass x, .ItemClass y) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System.Collections.Generic/Comparer.cs:86)
    3. System.Array.compare[ItemClass] (.ItemClass value1, .ItemClass value2, IComparer`1 comparer) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System/Array.cs:1744)
    4. System.Array.qsort[ItemClass,ItemClass] (.ItemClass[] keys, .ItemClass[] items, Int32 low0, Int32 high0, IComparer`1 comparer) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System/Array.cs:1721)
    5. System.Array.Sort[ItemClass,ItemClass] (.ItemClass[] keys, .ItemClass[] items, Int32 index, Int32 length, IComparer`1 comparer) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System/Array.cs:1674)
    6. Rethrow as InvalidOperationException: The comparer threw an exception.
    7. System.Array.Sort[ItemClass,ItemClass] (.ItemClass[] keys, .ItemClass[] items, Int32 index, Int32 length, IComparer`1 comparer) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System/Array.cs:1677)
    8. System.Array.Sort[ItemClass] (.ItemClass[] array, Int32 index, Int32 length, IComparer`1 comparer) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System/Array.cs:1623)
    9. System.Collections.Generic.List`1[ItemClass].Sort () (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System.Collections.Generic/List.cs:568)
    10. MyMimic.sortInventory () (at Assets/scripts/MyMimic.cs:154)
    11. MyMimic.OnGUI () (at Assets/scripts/MyMimic.cs:68)
    The code I use is

    Code (csharp):
    1. inventory.Sort();
    Could you guys help me sort out the Sort() function of my inventory list ? That would be awesome :)

    Cheers!

    Dawnreaver
     
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    1. Make your items comparable with other items the way you like:

    Code (csharp):
    1. internal class Item : IComparable<Item>
    2. {
    3.     public string itemName;
    4.  
    5.     // ...
    6.  
    7.     public int CompareTo(Item other)
    8.     {
    9.         return itemName.CompareTo(other.itemName);
    10.     }
    11. }
    and then just call inventory.Sort().

    2. Or sort the collection this way:

    Code (csharp):
    1. inventory.Sort((a, b) => a.itemName.CompareTo(b.itemName));
     
    MerlinsMaster and Tim_Harvey like this.
  3. Dawnreaver

    Dawnreaver

    Joined:
    Oct 13, 2010
    Posts:
    131
    Code (csharp):
    1. inventory.Sort((a, b) => a.itemName.CompareTo(b.itemName));
    [/QUOTE]

    You Sir, diserve a medal! Thanks a lot! All other stuff I found online was rather confusing. But this is so clear! Thanks a lot! And yes, it works like a treat :)