2
\$\begingroup\$

I am trying to create a crafting panel that sorts the list of items you can make alphabetically, yet I cannot figure out why .Sort() is not working.

What I have is a game object that holds a list of all the items you can make and the requirements, then in my crafting script I access the game object for the list and display the info.

Everything works fine until I try to sort anything. craftDatabase is what I need to be sorted. Also, it needs to be sorted by ItemID.


using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CraftDatabase : MonoBehaviour 
{
    public List<Blueprint> craftDatabase = new List<Blueprint>();
}

[System.Serializable]
public class Blueprint
{
    public string itemId;
    public int itemAmount;
    public float itemCraftTime = 1f;
    public CraftPanel.subsections subsection;
    public CraftPanel.workstations workStation;
    public List<requiredItem> requiredItems = new List<requiredItem>();

    [HideInInspector]        
    public string desc;

    [System.Serializable]
    public struct requiredItem
    {
        public string itemId;
        public int itemAmount;
    }

    public Blueprint()
    {
    }
}

When I try to throw in craftDatabase.craftDatabase.Sort(); it gives me this error:

ArgumentException: does not implement right interface

How do I fix this error to use Sort()?

\$\endgroup\$
4
  • \$\begingroup\$ I updated it with the craftdatabase script \$\endgroup\$ – rackemup420 Dec 23 '16 at 15:58
  • \$\begingroup\$ @Gnemlock We tolerate generic programming questions when they're in the context of game programming. I don't remember where the post about it was. \$\endgroup\$ – Vaillancourt Jan 5 '17 at 11:22
  • \$\begingroup\$ @Gnemlock Yeah, that's a while ago :) I've grown up since. I agree that the use should have searched google first, though. \$\endgroup\$ – Vaillancourt Jan 5 '17 at 11:58
  • \$\begingroup\$ I have asked on meta, and there was no change as per @AlexandreVaillancourt comment, but we are in the process of change. Ultimately, I have been so confident in the root of this question because I know the answer. I can also identify a fair amount of redundancy, in your question (we only need to see Blueprint to see why Sort() is not working) so as such, I am removing the additional examples to further clean up the question (debugging questions require a minimal viable example). \$\endgroup\$ – Gnemlock Jan 5 '17 at 21:55
2
\$\begingroup\$

Before we can use Sort(), we need to know how to sort between instances of the type we are using. The error is complaining that you do not have the required sorting functions, which are sought out through an interface.

An interface is basically a set of method signatures. Any class that uses that interface must implement the same methods. If you are unfamiliar with using interfaces, I suggest you follow this Unity scripting tutorial.

In this context, this allows us to take a generic type and guarantee that it can be sorted; we can write our own methods to handle comparing two instances.


According to official documentation, you need to implement either IComparable or IComparable<T>. The key difference is that the inclusion of <T> makes the method generic and more adaptable. This also makes it more confusing, and it looks like we do not need to use generics, so let's use IComparable.


According to official documentation, to implement IComparable, we only need to implement CompareTo(Object). This method must return an int value, which represents the approximate compared order of the local Object and the Object that was passed in.

  • If the local Object is greater, we need to return a positive number.
  • If the passed in Object is greater, we need to return a negative number.
  • If the two Object types are equal, we must return zero.

This actually becomes quite easy, in context. You want to compare a String, which should already implement a variation of CompareTo().

To allow sorting in a generic list, your Blueprint class should look as follows:

class Blueprint : IComparable
{
    public string itemID;

    ...

    public override int CompareTo(Blueprint other)
    {
        return itemID.CompareTo(other.itemID);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Please let me know if there are any problems. It's really hard to test this sort of thing, ATM. \$\endgroup\$ – Gnemlock Jan 5 '17 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.