Sorting

AP Computer Science A· difficulty 3/5

public class Item implements Comparable<Item> {
  private int weight;
  public Item(int w) { weight = w; }
  public int compareTo(Item other) {
    return other.weight - this.weight;
  }
}

What sort order does this produce?

  • A

    ascending by weight

  • B

    random

  • C

    no order

  • D

    descending by weight

    check_circle

Explanation

Reversing the subtraction yields descending order.

Want 10 more like this — adaptive to your weak spots?

Related questions