compareValues static method

int compareValues(
  1. dynamic a,
  2. dynamic b,
  3. bool ascending
)

Compare two dynamic values with a documented total order.

Nulls sort last regardless of direction. Values of the same type compare naturally. Mixed types compare by type rank: numbers, then strings, then booleans, then everything else (by toString), so a mixed column sorts numbers before their string look-alikes instead of comparing "10" < "9" lexicographically.

Implementation

static int compareValues(dynamic a, dynamic b, bool ascending) {
  final multiplier = ascending ? 1 : -1;
  if (a == null && b == null) return 0;
  if (a == null) return 1;
  if (b == null) return -1;
  final rankA = _typeRank(a);
  final rankB = _typeRank(b);
  if (rankA != rankB) return (rankA - rankB) * multiplier;
  if (a is num && b is num) return a.compareTo(b) * multiplier;
  if (a is String && b is String) return a.compareTo(b) * multiplier;
  if (a is bool && b is bool) {
    return (a == b ? 0 : (a ? 1 : -1)) * multiplier;
  }
  return a.toString().compareTo(b.toString()) * multiplier;
}