inputOutputComparator function

int inputOutputComparator(
  1. InputOutput a,
  2. InputOutput b
)

comparator function that allows sorting trades by their output amounts, in decreasing order, and then input amounts in increasing order. i.e. the best trades have the most outputs for the least inputs and are sorted first

Implementation

int inputOutputComparator(InputOutput a, InputOutput b) {
  assert(a.inputAmount.currency == b.inputAmount.currency);
  assert(a.outputAmount.currency == b.outputAmount.currency);

  if (a.outputAmount == b.outputAmount) {
    if (a.inputAmount == b.inputAmount) {
      return 0;
    }
    // trade A requires less input than trade B, so A should come first
    if (a.inputAmount.raw < b.inputAmount.raw) {
      return -1;
    } else {
      return 1;
    }
  } else {
    // tradeA has less output than trade B, so should come second
    if (a.outputAmount.raw < b.outputAmount.raw) {
      return 1;
    } else {
      return -1;
    }
  }
}