bad comparison function; sorting will be broken

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By exuin
func sort_height_descending(a: Array, b: Array) -> bool:
    if a[1].get_height() > b[1].get_height():
        return true
    elif a[1].get_height() < b[1].get_height():
        return false
    elif a[1].get_width() > b[1].get_width():
        return true
    elif a[1].get_width() < b[1].get_width():
        return false
    else:
        return true

What do?

:bust_in_silhouette: Reply From: jgodfrey

I assume this is supposed to be a sort_custom() function for an Array (as documented here)?

If so, the docs show the function should be static.

Using your function as-is seems to work for me - after making it static. However, I don’t really understand what you’re doing.

You seem to be trying to sort an Array of Array’s that contain some sort of object that has get_height() and get_width() functions (such as an Image).

Further, you’re basing the sort decision on get_height() and get_width() results of the 2nd element in the array. Again, not sure of the intention here, but the above does seem to work for me (or, at least generates a modified order for my random array and does not generate the error you reported).

If the above doesn’t help, perhaps a more detailed explanation, along with some more code that shows how you’re setting up and calling the custom sort.

Changing the func to static did not change anything.

I am trying to make a sprite packer. I am sorting the sprites from largest to smallest, height first then width. (The first element of the array is a TreeItem and is irrelevant.)

exuin | 2022-08-24 16:04

Can you post some code showing how the custom sort function is being called?

jgodfrey | 2022-08-24 16:29

var sorted_sprites := []
for key in sprites:
	sorted_sprites.append([key, sprites[key][0]])
if max_size.selected == 0: # Max width, sort by height
	sorted_sprites.sort_custom(self, "sort_height_descending")

exuin | 2022-08-24 16:47

:bust_in_silhouette: Reply From: exuin
func sort_height_descending(a: Array, b: Array) -> bool:
	if a[1].get_height() > b[1].get_height():
		return true
	elif a[1].get_height() < b[1].get_height():
		return false
	elif a[1].get_width() > b[1].get_width():
		return true
	else:
		return false

works but i don’t know why

Ah, yeah, you’re correct - that static doesn’t seem to be necessary. And, again, I’m not seeing the error you reported using a copy/pasted instance of your function. Perhaps my dummy array isn’t exercising the function well enough…

And, in the function that works , you’ve just dropped the last elif clause and changed the return value of the else clause (that’s what it looks like at a glance)?

jgodfrey | 2022-08-24 17:01

yeah, since the example had return false as the last clause

exuin | 2022-08-24 17:23

:bust_in_silhouette: Reply From: FranzShester

I’ve stepped into the same issue:

static func sort_algorithm(unit_a: Unit, unit_b: Unit) -> bool:
	
	var agility_a = unit_a.agility
	var agility_b = unit_b.agility
	
	var percentage:= randf_range(0.875, 1.125)
	percentage = snapped(percentage, 0.001)
	
	agility_a = (agility_a * percentage)
	agility_b = (agility_b * percentage)
	
	var x = randi_range(1, -1)
	var y = randi_range(1, -1)
	
	agility_a = agility_a + x
	agility_b = agility_b + y

	if agility_a > agility_b:
		return true
	else:
		return false

I’ve performed many tests and I’ve realized that the error triggers randomly when using randi_range(1, -1), I;ve also tried replacing that function for randi() % 2 and the problem doesn’t happen if I use that once only… I’ve no idea why is that…