Forgive me for this toy example. I’m not completely used to C# Generics so I wasn’t completely sure how to make a Generic version of say MixedType<T, U> where T contains the interface IComparable and U contains the interface IComparable
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class __TestScript300 : MonoBehaviour
{
public int itemCount = 15;
List<MixedData> myMixedData;
class MixedData : IComparable<MixedData>
{
public string someString;
public int someInt;
public MixedData(string someString, int someInt)
{
this.someInt = someInt;
this.someString = someString;
}
public int CompareTo(MixedData other)
{
return someInt.CompareTo(other.someInt);
}
public override string ToString()
{
return someInt + ": " + someString;
}
}
class SortMixedDataByString : IComparer<MixedData>
{
public int Compare(MixedData left, MixedData right)
{
return left.someString.CompareTo(right.someString);
}
}
void Start()
{
myMixedData = new List<MixedData>(itemCount);
Debug.Log("-------------\nBefore sorting our custom type...");
for (int i = 0; i < itemCount; i++)
{
float _a = UnityEngine.Random.Range(101f, 400f);
float _b = UnityEngine.Random.Range(101f, 400f);
float _c = UnityEngine.Random.Range(101f, 400f);
myMixedData.Add(new MixedData("<" +_a + ", " + _b + ", " + _c + ">", (int)UnityEngine.Random.Range(1f, 100f)));
Debug.Log(myMixedData[i]);
}
myMixedData.Sort();
Debug.Log("-------------\nAfter sorting our custom type based on its default sorting method...");
for (int i = 0; i < myMixedData.Count; i++)
Debug.Log(myMixedData[i]);
myMixedData.Sort(new SortMixedDataByString());
Debug.Log("-------------\nAfter sorting our custom type based on its other parameter");
for (int i = 0; i < myMixedData.Count; i++)
Debug.Log(myMixedData[i]);
}
}
The custom class inherits from IComparable so we can properly compare against our custom type when performing a List.Sort operation. If the custom type doesn’t inherit from IComparable, I don’t know if C# will sort the values (I doubt it, but I could be wrong). By explicitly defining how the sort should be done, you have more control over the ordering of your custom class.
The other method of sorting in this scenario is with an object instance that inherits from ICompararer. This method allows you to override the default sorting behavior for your custom type, giving you even more options.
Edit: It doesn’t look like your custom type needs to inherit from IComparable at all, as long as you make an IComparer for your type and supply it to the List at the call of List.Sort
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class __TestScript301 : MonoBehaviour
{
public int itemCount = 15;
List<DoesntInheritIComparable> otherList;
class DoesntInheritIComparable
{
public string someString;
public int someInt;
public DoesntInheritIComparable(string someString, int someInt)
{
this.someInt = someInt;
this.someString = someString;
}
public override string ToString()
{
return someInt + ": " + someString;
}
}
class SortOtherDataByInt : IComparer<DoesntInheritIComparable>
{
public int Compare(DoesntInheritIComparable left, DoesntInheritIComparable right)
{
return left.someInt.CompareTo(right.someInt);
}
}
void Start()
{
otherList = new List<DoesntInheritIComparable>();
Debug.Log("-------------\nBefore sorting our custom type...");
for (int i = 0; i < itemCount; i++)
{
float _a = UnityEngine.Random.Range(101f, 400f);
float _b = UnityEngine.Random.Range(101f, 400f);
float _c = UnityEngine.Random.Range(101f, 400f);
otherList.Add(new DoesntInheritIComparable("<" + _a + ", " + _b + ", " + _c + ">", (int)UnityEngine.Random.Range(1f, 100f)));
Debug.Log(otherList[i]);
}
Debug.Log("-----------\nSorting our type that doesn't inherit from IComparable by using an IComparer");
otherList.Sort(new SortOtherDataByInt());
for (int i = 0; i < otherList.Count; i++)
Debug.Log(otherList[i]);
}
}
If you’re never planning on sorting by more than one type (for example, you only plan to sort by integers) then you only need one class that follows the IComparer interface for your custom type.