using System.Collections.Generic;
SortedDictionary<CustomKey, string> dict = new SortedDictionary<CustomKey, string>();
dict.Add(new CustomKey() { intKey = 1, sortingVariable = 8 }, "1");
dict.Add(new CustomKey() { intKey = 2, sortingVariable = 6 }, "2");
dict.Add(new CustomKey() { intKey = 5, sortingVariable = 2 }, "expected value");
dict.Add(new CustomKey() { intKey = 3, sortingVariable = 5 }, "3");
dict.Add(new CustomKey() { intKey = 4, sortingVariable = 4 }, "4");
dict.TryGetValue(new CustomKey() { intKey = 5, sortingVariable = 17} , out res);
public struct CustomKey : IComparable<CustomKey>
public override bool Equals(object obj)
return ((CustomKey)obj).intKey == intKey && ((CustomKey)obj).objectKey == objectKey;
public override int GetHashCode()
return intKey.GetHashCode() ^ objectKey.GetHashCode();
public int CompareTo(CustomKey obj)
if (obj.intKey == intKey) return 0;
return sortingVariable.CompareTo(obj.sortingVariable);
public int sortingVariable;
public string otherSortingVariable;