prioritize method Null safety

int prioritize(
  1. Version a,
  2. Version b
)

Compares a and b to see which takes priority over the other.

Returns 1 if a takes priority over b and -1 if vice versa. If a and b are equivalent, returns 0.

Unlike compareTo, which orders versions, this determines which version a user is likely to prefer. In particular, it prioritizes pre-release versions lower than stable versions, regardless of their version numbers. Pub uses this when determining which version to prefer when a number of versions are allowed. In that case, it will always choose a stable version when possible.

When used to sort a list, orders in ascending priority so that the highest priority version is last in the result.

Implementation

static int prioritize(Version a, Version b) {
  // Sort all prerelease versions after all normal versions. This way
  // the solver will prefer stable packages over unstable ones.
  if (a.isPreRelease && !b.isPreRelease) return -1;
  if (!a.isPreRelease && b.isPreRelease) return 1;

  return a.compareTo(b);
}