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

  • Home
  • SEARCH
  • 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 4242122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:24:00+00:00 2026-05-21T03:24:00+00:00

I recently came across a comparator that on the surface looks incorrect. However, I’ve

  • 0

I recently came across a comparator that on the surface looks incorrect. However, I’ve been unable to come up with an input to it that causes the comparator to produce the wrong result.

It incorrectly treats values as equal if o1 <= o2, and correctly returns 1 if o1 > o2.

I’ve tried to simplify the scenario as much as possible below. Can anyone either:

  1. Explain why this is ok.
  2. Produce an input array that causes it to sort the output incorrectly.

I’ve experimented with it quite a a bit and I’m throwing in the towel!

package comparator;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class BadComparator implements Comparator<Integer>
{
    public int compare(Integer o1, Integer o2)
    {
        // Generally Accepted implementation
        //return o1 - o2; 

        // Incorrect(?) Implementation
        return (o2 < o1) ? 1 : 0;
    }

    public static void main(String[] args)
    {
        List<Integer> intList = Arrays.asList(10, 9, 8, 1, 2, 3, 7, 4, 5, 6);
        Collections.sort(intList, new BadComparator());
        System.out.println(intList);
    }
}
  • 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-05-21T03:24:01+00:00Added an answer on May 21, 2026 at 3:24 am

    Collections.sort is implemented as mergesort. looking at the source, all compare conditions are >0 or <=0 which by chance treat the negative case the same as the equal case. a different sort implementation can fail.

    Per jbellis’s comment, “It’s not quite “just luck,” though — Collections.sort is guaranteed to be “stable,” meaning that equal elements must be in the same relative order post-sort. I’m not sure it’s impossible to come up with a stable sort implementation that fails with this comparator, but I can’t think of one off the top of my head.“

    private static void mergeSort(Object[] src,
                  Object[] dest,
                  int low, int high, int off,
                  Comparator c) {
    int length = high - low;
    
    // Insertion sort on smallest arrays
    if (length < INSERTIONSORT_THRESHOLD) {
        for (int i=low; i<high; i++)
        for (int j=i; j>low && c.compare(dest[j-1], dest[j])>0; j--)
            swap(dest, j, j-1);
        return;
    }
    
        // Recursively sort halves of dest into src
        int destLow  = low;
        int destHigh = high;
        low  += off;
        high += off;
        int mid = (low + high) >> 1;
        mergeSort(dest, src, low, mid, -off, c);
        mergeSort(dest, src, mid, high, -off, c);
    
        // If list is already sorted, just copy from src to dest.  This is an
        // optimization that results in faster sorts for nearly ordered lists.
        if (c.compare(src[mid-1], src[mid]) <= 0) {
           System.arraycopy(src, low, dest, destLow, length);
           return;
        }
    
        // Merge sorted halves (now in src) into dest
        for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
            if (q >= high || p < mid && c.compare(src[p], src[q]) <= 0)
                dest[i] = src[p++];
            else
                dest[i] = src[q++];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across some code that looks something like this: <head> <?php /*
I recently came across an IE7 only bug that I thought I'd share so
I recently came across a ASP 1.1 web application that put a whole heap
I recently came across a Windows library called AHK that gives me great control
I recently came across some Java code that simply put some strings into a
I recently came across a html email that contained _label attributes on the <
I recently came across the Kogut language , and was interested by it. However,
I recently came across a CSS pseudo class that can do an effect for
I recently came across a server that's sending a Location header (for a redirect)
I recently came across some weird looking class that had three constructors: class Class

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.