Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:46:18+00:00 2026-06-10T19:46:18+00:00

There is a code that I don’t really understand from Sedgewick’s Algorithms book, specifically

  • 0

There is a code that I don’t really understand from Sedgewick’s Algorithms book, specifically the part on Comparators. I understand very well how everything else works, e.g., the algorithm provided, the intention of using Comparators vs Comparable, etc. However, the one thing that I don’t seem to get are the parameters of the compare(Transaction v, Transaction w) method in the static class. For example, the array of Objects is passed and held by another Object reference in sort. When the helper method, less(), is invoked, the two elements of the Object array are held by another Object references. But this is what I don’t get: In

    public static class WhoOrder implements Comparator<Transaction> {
    public int compare(Transaction v, Transaction w) {
        return v.who.compareTo(w.who);
    }
}

The Object references seem to have been replaced by Transaction references. How is this so? With polymorphism, you’d have to use an explicit downcast, but this seems to bypass that. My guess is that it’s because of the parameterized type, but I don’t know why that is.

The class which contains a static class that implements Comparator

public class Transaction {
private final String  who;      // customer
private final Date    when;     // date
private final double  amount;   // amount

...


// ascending order of account number
public static class WhoOrder implements Comparator<Transaction> {
    public int compare(Transaction v, Transaction w) {
        return v.who.compareTo(w.who);
    }
}

public static void main(String[] args) {
    ...
    Arrays.sort(a, new Transaction.WhoOrder());
}

The sorting class:

// use a custom order and Comparator interface - see Section 3.5
public static void sort(Object[] a, Comparator c) {
    int N = a.length;
    for (int i = 0; i < N; i++) {
        for (int j = i; j > 0 && less(c, a[j], a[j-1]); j--) {
            exch(a, j, j-1);
        }
    }
}

// is v < w ?
private static boolean less(Comparator c, Object v, Object w) {
    return (c.compare(v, w) < 0);
}

// exchange a[i] and a[j]
private static void exch(Object[] a, int i, int j) {
    Object swap = a[i];
    a[i] = a[j];
    a[j] = swap;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:46:20+00:00Added an answer on June 10, 2026 at 7:46 pm

    WhoOrder is implemented as a Comparator with Transaction as its type parameter so the implementation of compare accepts two Transactions. However, the less method takes a raw Comparator (without a type). For backwards compatibility when you do this the generics type checking is disabled so you are able to pass 2 Objects to compare.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is something that I really don't understand with the HttpListener. The code below
I am learning jQuery from book Learning jQuery, and found part of code that
There is some code that I'm trying to convert from IList to IEnumerable :
I am new to creating plug-ins, and there's much that I don't understand. One
I've come across some C code that I don't quite understand. The following compiles
Ok, I've got a bit of code that I don't like very much. Given
In a .f file there is code that does this: real Bob, avar ...
I'm currently reading Rails 3 In Action. There is code that I was wondering
There is some code that I am using written by someone else that I
There's a code that reads a file and makes some calculating operations with its

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.