CommandHelper

CommandHelper

46.5k Downloads

array_sort using closure with only a single associative array entry destroys the array

LadyCailinBot opened this issue ยท 1 comments

commented

CMDHELPER-3113 - Reported by pr4xt3roy

When I use the array_sort to sort an array of associative arrays and there is only ONE entry, the array becomes empty!

Here's how you can reproduce it:

@array = array( array(name: 'Jill', age: 19) );
msg("Before sort: @array");
array_sort(@array, closure(@left, @right){
    return(@left['age'] > @right['age']);
  });
msg("After sort: @array");

Output:

2015-12-14 16:51:43 [INFO] Before sort: {{age: 19, name: Jill}}
2015-12-14 16:51:43 [INFO] After sort: {}

I expect the array after the sort to contain the same amount of items as before the sort.

commented

Comment by PseudoKnight

Took me a moment to see the problem. It actually checks if the array size is <= 1, but it returns a reference instead of a cloned array. Then the original is cleared and populated with the new sorted array, but that sorted array is the same reference that was cleared. I just need to move the check.