Page MenuHomeFeedback Tracker

SortAttribute on vector value returns incorrect ordering
Reviewed, NormalPublic

Description

I was updating my code to use the new SortAttribute so I can sort an array of complex types by the vector member they have.
On normal array<vector> the ordering returned by Sort is exactly what I would like and I was expecting the SortAttribute to do the same.
However, as the code below shows, the ordering does not behave the same when sorting my complex type. The ordering does not appear to make any sense as well.

Perhaps vector is currently not a supported type in SortAttribute and thus the sort logic falls back to sort the array by pointer arithmetic or the wrappers instead? I can not explain how an ordering of 3 0 1 could otherwise happen. No matter if ASC or DESC it should be impossible in any kind of sorting operation.

class EDF_SortWrapper
{
	[SortAttribute()]
	vector m_SortValue;

	//------------------------------------------------------------------------------------------------
	void EDF_SortWrapper(vector sortValue)
	{
		m_SortValue = sortValue;
	}
}

//------------------------------------------------------------------------------------------------
static void Reproduce()
{
    array<ref EDF_SortWrapper> data = {
        new EDF_SortWrapper("1 9 1"),
        new EDF_SortWrapper("1 2 1"),
        new EDF_SortWrapper("0 9 9"),
        new EDF_SortWrapper("3 0 0"),
    };
    data.Sort(false);
    foreach (auto entry : data)
    {
        Print(entry.m_SortValue);
    }

    array<vector> data2 = {"1 9 1", "1 2 1", "0 9 9", "3 0 0"};
    data2.Sort(false);
    data2.Debug();
}

SCRIPT : vector m_SortValue = <3.000000,0.000000,0.000000>
SCRIPT : vector m_SortValue = <0.000000,9.000000,9.000000>
SCRIPT : vector m_SortValue = <1.000000,2.000000,1.000000>
SCRIPT : vector m_SortValue = <1.000000,9.000000,1.000000>
SCRIPT : Array count: 4
SCRIPT : [0] => <0.000000,9.000000,9.000000>
SCRIPT : [1] => <1.000000,2.000000,1.000000>
SCRIPT : [2] => <1.000000,9.000000,1.000000>
SCRIPT : [3] => <3.000000,0.000000,0.000000>

Details

Severity
Major
Resolution
Open
Reproducibility
Always
Operating System
Windows 10 x64
Category
General

Event Timeline

Arkensor updated the task description. (Show Details)Jul 27 2023, 11:20 AM
Geez changed the task status from New to Assigned.Jul 28 2023, 10:44 AM
Geez changed the task status from Assigned to Reviewed.Aug 14 2023, 10:40 AM
Geez added a subscriber: Geez.

Hello Arkensor.
This is the developer response: Hello, SortAttribute does not support vector type. From SortAttribute doc: "Supported member types for sorting are int, float, string and object".

Ok, then please support it. Sorting vectors in any reliable order is better than the current results. Sort by first coordinate if same then second, if same then third. So the same ordering that they would have if converted to a string first and then sorted by alphanumeric order.