Dart BigInt compareTo()
Syntax & Examples

BigInt.compareTo() method

The `compareTo()` method compares this BigInt to another BigInt.


Syntax of BigInt.compareTo()

The syntax of BigInt.compareTo() method is:

 int compareTo(BigInt other) 

This compareTo() method of BigInt compares this to other.

Parameters

ParameterOptional/RequiredDescription
otherrequiredthe BigInt to compare to

Return Type

BigInt.compareTo() returns value of type int.



✐ Examples

1 Compare two positive BigInts

In this example,

  1. We create two BigInts, bigInt1 with a value of 10 and bigInt2 with a value of 5.
  2. We use the compareTo() method to compare bigInt1 with bigInt2.
  3. We print the comparison result to standard output.

Dart Program

void main() {
  BigInt bigInt1 = BigInt.from(10);
  BigInt bigInt2 = BigInt.from(5);
  int comparison = bigInt1.compareTo(bigInt2);
  print('Comparison result: $comparison');
}

Output

Comparison result: 5

2 Compare two equal BigInts

In this example,

  1. We create two equal BigInts, bigInt1 and bigInt2, both with a value of 100.
  2. We use the compareTo() method to compare bigInt1 with bigInt2.
  3. We print the comparison result to standard output.

Dart Program

void main() {
  BigInt bigInt1 = BigInt.from(100);
  BigInt bigInt2 = BigInt.from(100);
  int comparison = bigInt1.compareTo(bigInt2);
  print('Comparison result: $comparison');
}

Output

Comparison result: 0

3 Compare two negative BigInts

In this example,

  1. We create two BigInts, bigInt1 with a value of -5 and bigInt2 with a value of -10.
  2. We use the compareTo() method to compare bigInt1 with bigInt2.
  3. We print the comparison result to standard output.

Dart Program

void main() {
  BigInt bigInt1 = BigInt.from(-5);
  BigInt bigInt2 = BigInt.from(-10);
  int comparison = bigInt1.compareTo(bigInt2);
  print('Comparison result: $comparison');
}

Output

Comparison result: 5

Summary

In this Dart tutorial, we learned about compareTo() method of BigInt: the syntax and few working examples with output and detailed explanation for each example.