flutterdartdatedatetimecompareto

Compare to with String in flutter


I have just checked some cases with CompareTo method of String as below:

  print("case 1>>>>> ");
  print("02 Dec 2022".compareTo("24 Nov 2022"));

  print("case 2>>>>> ");
  print("24 Nov 2022".compareTo("24 Nov 2022"));

  print("case 3>>>>> ");
  print("23 Nov 2022".compareTo("24 Nov 2022"));

For case 2, it returned value 0 For case 1 and case 3 It returned value -1

Why it is returning value -1 for both case 1 and 3?


Solution

  • You can find on compareTo

    ... it returns a negative integer if this is ordered before other, a positive integer if this is ordered after other, and zero if this and other are ordered together.

    The case 1 and 3 are, this is ordered before other.

    For case 2, this and other are ordered together.