Core Java · Chapter 29 of 48

Sorting with Comparator

By the end of this chapter you can give a class its natural order, sort a list any other way with a comparator, and handle ties and missing values without a crash. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Core Java
  • 5 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

Anjali wants the NDLS menu board cheapest first, so people in a hurry see the cutting chai. Ravi wants the shift's orders sorted three ways: biggest first for the manager, by station for the auditors, by customer name for the loyalty team. Walk-in customers have no name, and they must go to the bottom without crashing anything.

With what you know so far, you would write a sorting loop for each of those orders: four loops that swap elements by hand, each comparing different fields, each a chance to get a sign wrong. Java can already sort; what it needs from you is only the rule for which of two elements comes first.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    MenuItem.java: a natural order, cheapest first

    In MenuItem.java, make compareTo(other) return a negative number when this item comes before other, a positive number when it comes after, and exactly 0 for an item with the same price and name. Lower price comes first; when the prices are equal, the names decide, in String's natural order (A to Z).

    For example, Cutting chai at 25 comes before Masala chai at 40, Bun maska at 50 comes before Veg puff at 50, and a menu of Veg puff, Masala chai, Bun maska and Cutting chai sorts as Cutting chai, Masala chai, Bun maska, Veg puff.

  2. Task 2

    Sorting.java: biggest order first, ties in arrival order

    In Sorting.java, make byAmountDesc(orders) return a new list of the orders sorted by amount, biggest first. Orders with the same amount keep the order they had in the given list. The given list is not changed (it may be unmodifiable). An empty list gives an empty list.

    For example, the shift with tokens 1 to 7 and amounts 40, 90, 90, 25, 40, 150, 40 gives tokens 6, 2, 3, 1, 5, 7, 4.

  3. Task 3

    Sorting.java: by station, then biggest order first in each

    In Sorting.java, make byStation(orders) return a new list of the orders sorted by station code A to Z, and inside one station by amount, biggest first. The given list is not changed.

    For example, the shift gives CSMT 40, CSMT 25, NDLS 90, NDLS 40, SBC 150, SBC 90, SBC 40, which is tokens 7, 4, 2, 5, 6, 3, 1.

  4. Task 4

    Sorting.java: by customer name, walk-ins last

    In Sorting.java, make byCustomer(orders) return a new list of the orders sorted by customer name A to Z, ignoring upper and lower case. Orders with no customer (a null name) go last, in the order they had in the given list. The given list is not changed.

    For example, the shift's names ravi, null, Anjali, meera, Kabir, null, Dev give Anjali, Dev, Kabir, meera, ravi, then the two walk-ins, tokens 2 and 6: tokens 3, 7, 5, 4, 1, 2, 6.

  5. Task 5 · interview question

    Interview: Sorting.java, fix the subtraction that overflows

    In Sorting.java, make compareAmounts(a, b) return a negative number when a is less than b, exactly 0 when they are equal, and a positive number when a is greater, for every pair of int values, including the largest and smallest ints. The starter's a minus b overflows for big values.

    For example, 25 and 40 give a negative number and 40 and 40 give 0; -2000000000 and 2000000000 give a negative number; Integer.MIN_VALUE and 1 give a negative number; Integer.MAX_VALUE and -1 give a positive number.

What you will learn

  • Comparable: a class's one natural order, in its compareTo method.
  • Comparator: any other order, built from keys with comparing, thenComparing and reversed.
  • Ties and missing values: stable sorting, case-insensitive names and nullsFirst or nullsLast.
  • Why not subtract: the overflow that breaks a - b.

More in Core Java

Build "Sorting with Comparator" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.