Skip to content

array.sort behavior can differ between MSVC and GCC compilers #1347

Closed as not planned

Description

@NotHyper-474
class Main {
	static function main() {
		var sortThing:Array<Null<Int>> = [0, 0, 1000, null, 800, 801, 802, 850, 850, 900, 1000, 1001, 10000, 10000, 10000];

		sortThing.sort((a, b) -> {
			if (a == null || b == null) return 0;
			return Reflect.compare(a, b);
		});

		trace(sortThing);
	}
}

I've noticed this behaved differently between the compilers.
On MSVC, sorting results in [0,0,1000,null,800,801,802,850,850,900,1000,1001,10000,10000,10000], but on GCC the same array instead becomes [0,0,850,1000,null,800,801,802,850,900,1000,1001,10000,10000,10000].

It's a minimal recreation of how objects in a game's scene are ordered, which also caused a bug.
While it uncovers a flaw with how the game's sorting objects, I considered that the discrepancy in how the sorting behaved between compilers could be a bug.

Activity

  1. Aidan63 commented on Jun 8, 2026

    @Aidan63
    Contributor

    This will be because your comparison function doesn't adhere to "weak strict ordering", under the hood hxcpp's array sort uses std::sort which has this requirement. Using a comparison function which doesn't follow this is undefined behaviour per the cpp spec, this can result in inconsistent results, random values in the results, or crashes.

    https://en.cppreference.com/cpp/named_req/Compare

    We discovered this during some coroutine work and I think we decided to make haxe follow the same requirements, but seems we forgot to update the docs.

  2. tobil4sk commented on Oct 30, 2026

    @tobil4sk
    Member

    Looks like this PR was related: #1416. Seems haxe docs have still not been updated

    Also, for more context, here was the hxcoro discussion: Aidan63/haxe#154 (comment)

    This is also a duplicate of #961.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions