I would like to be able to have an array of a common base class and sort it. Each complex element shall be sorted by the same type e.g. all of them via int, but on one instance it might be a different field than on the other - or the code is generalized for wrappers of different types (e.g. all hold floats, or all hold strings) and one wants to write re-usable logic that is the same, including the sort, just getting the sort values is specialized via templates. Right now it appears that the sorting resolves at runtime IF and which property will be accessed for sorting. So the code below sees no sort attribute for EDF_SortWrapperBase and defaults to sort by pointer value. Instead, I would like the code to check the instance type of each item, see if there is a property annotated with SortAttribute and then use that to fetch the sort value for that item. I know this might be a bit slower but one could maybe opt into this with an optional parameter. As it stands the alternative for me is to extract the values into one typed array per possible template typename, sort it, and then do expensive map lookups again to match sort values to the wrappers that provided them. That will be much more expensive, so I prefer to switch to a more streamlined C++ handling once it becomes available. Thank you!
class EDF_SortWrapperBase { } class EDF_SortWrapperT<Class T> : EDF_SortWrapperBase { [SortAttribute()] T m_SortValue; //------------------------------------------------------------------------------------------------ void EDF_SortWrapperT(T sortValue) { m_SortValue = sortValue; } } //------------------------------------------------------------------------------------------------ static void Reproduce() { array<ref EDF_SortWrapperBase> data = { new EDF_SortWrapperT<int>(5), new EDF_SortWrapperT<int>(1), new EDF_SortWrapperT<int>(3), new EDF_SortWrapperT<int>(7), }; data.Sort(false); foreach (auto entry : data) { Print(EDF_SortWrapperT<int>.Cast(entry).m_SortValue); } }
The above code should return:
SCRIPT : int m_SortValue = 1
SCRIPT : int m_SortValue = 3
SCRIPT : int m_SortValue = 5
SCRIPT : int m_SortValue = 7