Comp 110 Sorting with Arrays

What does sorting look like in arrays?

When sorting with arrays, we make use of the Comparator<T> Functional Interface and three constants called A_BEFORE_B, A_SAME_AS_B, and A_AFTER_B, described here.

Both 'ascending' and 'descending' sorting algorithms use a similar process. They compare every part of items in an array, returning one of the designated constants mentioned above. Our job is to write the comparator function that will determine how the array will be sorted. If you are interested in more advanced sorting and searching algorithms, consider continuing on to COMP 401 and 410!

Writing a Comparator Function

//here is an example of an 'ascending' comparator function!
let ascending = (a: number, b: number): number => {
    if (a < b) {
        return A_BEFORE_B;
    } else if (a === b) {
        return A_SAME_AS_B;
    } else {
        return A_AFTER_B;
    }
};