Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom_sort always returns errors once > 16 identical values are present in the array to be sorted #49618

Open
2shady4u opened this issue Jun 15, 2021 · 5 comments

Comments

@2shady4u
Copy link
Contributor

2shady4u commented Jun 15, 2021

Godot version

3.3.2

System information

Windows 10

Issue description

Custom sorting an array of 16+ identical values returns several errors.

ERROR: partitioner: bad comparison function; sorting will be broken
   At: ./core/sort_array.h:183
ERROR: partitioner: bad comparison function; sorting will be broken
   At: ./core/sort_array.h:190
ERROR: unguarded_linear_insert: bad comparison function; sorting will be broken
   At: ./core/sort_array.h:263

Even if this array is extended with other non-identical values, the issue persists.

Steps to reproduce

Basically add more than 16 identical values to an array and try to sort them.
I used this minimal code example:

extends Node2D

static func sort(a : int, b : int) -> bool:
	if a > b:
		return false
	else:
		return true

func _ready():
	var array := []
	for _i in range(0, 17):
		array.append(0)
	array.sort_custom(self, "sort")

Minimal reproduction project

custom_sorting_error.zip

@Zireael07
Copy link
Contributor

Just FYI, your zip didn't upload...

@2shady4u
Copy link
Contributor Author

oops :/ will fix asap

@akien-mga akien-mga added this to the 4.0 milestone Jun 15, 2021
@2shady4u 2shady4u changed the title custom_sort always returns errors once 17 identical values are present in the array to be sorted custom_sort always returns errors once >= 16 identical values are present in the array to be sorted Jun 15, 2021
@2shady4u
Copy link
Contributor Author

Okay, I correctly uploaded my zip now 😃
Hopefully someone can confirm this now without any problem 😝

@akien-mga
Copy link
Member

akien-mga commented Jun 15, 2021

I can confirm the issue in 3.x (8028122) and master (e312df0).

(To compile on master, change last line to array.sort_custom(sort).)

There's indeed a 16 threshold number in SortArray:

INTROSORT_THRESHOLD = 16

But what it means and why it triggers this issue, I don't know.

I checked if it could be a false positive introduced by the validation checks from #15536 (added in 3.1), but in 3.0.6 the bug seems reproducible too: <= 16 works ok, 17 triggers an error spam with errors such as these, or crashes:

SCRIPT ERROR: sort: Invalid operands 'NodePath' and 'int' in operator '>'.
   At: res://Node2D.gd:4.
SCRIPT ERROR: sort: Invalid operands 'int' and '' in operator '>'.
   At: res://Node2D.gd:4.
SCRIPT ERROR: sort: Invalid operands 'Nil' and 'int' in operator '>'.
   At: res://Node2D.gd:4.
SCRIPT ERROR: sort: Invalid operands 'int' and '' in operator '>'.
   At: res://Node2D.gd:4.

@2shady4u 2shady4u changed the title custom_sort always returns errors once >= 16 identical values are present in the array to be sorted custom_sort always returns errors once > 16 identical values are present in the array to be sorted Jun 15, 2021
@YuriSizov YuriSizov modified the milestones: 4.0, 4.x Feb 23, 2023
@filipetavares
Copy link

I can confirm this still happens in 4.1.1 but in my case, this happens when there are more than 16 elements in an array, and the sort actually messes up the array previous order (but I'm guessing its because its not stable), as soon as I get the array to be lower than 16 elements it starts behaving correctly again.

For the moment I implemented a TimSort and I've solved my problem.
Just leaving the comment here to further bump this fix.

Keep up the good work.

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

No branches or pull requests

5 participants