How do I compare two characters in Dart?

Viewed 2997

I want to compare two characters. Something like this:

if ('a' > 'b') 

However, the above code is comparing two strings.

How do I do this in Dart?

2 Answers

String in Dart implements the Comparable interface. You can use compareTo to compare them.

String a = 'a';
String b = 'b';
String c = 'a';

print('value: ${a.compareTo(b)}');     // prints "value: -1"
print('value: ${a.compareTo(c)}');     // prints "value: 0"
print('value: ${b.compareTo(a)}');     // prints "value: 1"
Related