Please see drupal/core/lib/Drupal/Core/Form/FormBuilder.php:1038
// If one of the child elements has a weight then we will need to sort
// later.
unset($element['#sorted']);
This is incorrect as the children in $element will never be sorted if it is used together with drupal/core/lib/Drupal/Core/Render/Element/Table.php:343
foreach (Element::children($element[$first]) as $second) {
// Assign the element by reference, so any potential changes to the
// original element are taken over.
$column = array('data' => &$element[$first][$second]);
// Apply wrapper attributes of second-level elements as table cell
// attributes.
if (isset($element[$first][$second]['#wrapper_attributes'])) {
$column += $element[$first][$second]['#wrapper_attributes'];
}
$row['data'][] = $column;
}
See drupal/core/lib/Drupal/Core/Render/Element.php:72
// Do not attempt to sort elements which have already been sorted.
$sort = isset($elements['#sorted']) ? !$elements['#sorted'] : $sort;
This will always assigning $sort to FALSE.
Suggested fix is to replace unset($element['#sorted']); with $element['#sorted'] = FALSE;