radixSortFloat64WithNaN function

void radixSortFloat64WithNaN(
  1. Float64List data, {
  2. bool ascending = true,
  3. String nanPlacement = 'end',
  4. bool reuseBuffer = true,
})

Sorts a list of doubles with NaN handling options.

Provides fine-grained control over how NaN values are handled during sorting.

  • nanPlacement: Where to place NaN values:
    • 'start': NaNs at the beginning
    • 'end': NaNs at the end (default)
    • 'remove': Remove NaNs from the result

Example:

final data = Float64List.fromList([1.0, double.nan, 3.0, double.nan, 2.0]);
radixSortFloat64WithNaN(data, nanPlacement: 'end');
// Result: [1.0, 2.0, 3.0, NaN, NaN]

Implementation

void radixSortFloat64WithNaN(
  Float64List data, {
  bool ascending = true,
  String nanPlacement = 'end',
  bool reuseBuffer = true,
}) {
  if (data.length < 2) {
    return;
  }

  // Separate NaN values if needed
  if (nanPlacement == 'remove') {
    final nonNaN = <double>[];
    for (var i = 0; i < data.length; i++) {
      if (!data[i].isNaN) {
        nonNaN.add(data[i]);
      }
    }

    final temp = Float64List.fromList(nonNaN);
    radixSortFloat64(temp, ascending: ascending, reuseBuffer: reuseBuffer);

    // Copy back
    for (var i = 0; i < temp.length; i++) {
      data[i] = temp[i];
    }
    return;
  }

  // For 'start' or 'end', just sort normally and NaNs will be grouped
  radixSortFloat64(data, ascending: ascending, reuseBuffer: reuseBuffer);

  // If NaNs should be at start in ascending order, we need to rotate
  if (nanPlacement == 'start' && ascending) {
    _moveNaNsToStart(data);
  }
}