Identifying the custom compare pattern


There are many problems we need to sort sequences of primitive data types based on the ordering of a user-defined type that has no inherent ordering, or we may have to sort a sequence of some user-defined data type. These are generally easy or medium problems where we transform the primitive data type to some user-defined type using a transformation function t and apply some problem-specific rules to define the ordering between elements. The ultimate goal of the problem is usually to sort the original sequence according to the ordering of the transformed data.

If the problem statement or its solution follows the generic template below, it can be solved using the custom compare technique.

Template:

Given a sequence of data items and a transformation function t, sort the sequence using the transformed values of the elements.

Liking the course? Check our discounted plans to continue learning.