compareTo abstract method

int compareTo(
  1. Locale other
)

Compare locales for sorting.

This method implements Comparable interface to enable sorting of locale collections. Comparison is done hierarchically:

  1. First by language code
  2. Then by country code (empty treated as empty string)
  3. Finally by variant (empty treated as empty string)

Returns:

  • Negative value if this locale comes before other
  • Zero if locales are equal
  • Positive value if this locale comes after other

Example:

final locales = [
  Locale('fr', 'FR'),
  Locale('en', 'US'),
  Locale('en', 'CA'),
];

locales.sort((a, b) => a.compareTo(b));
// Result: [en-CA, en-US, fr-FR]

Implementation

int compareTo(Locale other);