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 7065087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:54:49+00:00 2026-05-28T04:54:49+00:00

I have the following program import java.util.*; public class Test { public static void

  • 0

I have the following program

import java.util.*;
public class Test {
    public static void main(String[] args) {
        Integer[] array = { 3, 1, 4, 1, 5, 9 };
        Arrays.sort(array, new Comparator<Integer>() {
            public int compare(Integer i1, Integer i2) {
                return i1 < i2 ? -1 : (i2 > i1 ? 1 : 0);
            }
        });
        System.out.println(Arrays.toString(array));
    }
}

This gives me the output [3, 1, 4, 1, 5, 9]. Why?

  • 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-28T04:54:50+00:00Added an answer on May 28, 2026 at 4:54 am

    You test for i1 < i2, and, if it fails, you test NOT for i1 > i2, but for i2 > i1.

    Change your comparator to

       return i1 < i2 ? -1 : (i1 > i2) ? 1 : 0;
    

    ADDED

    In an attempt to contribute something new to the two other answers that said the same thing and beat me to the punch, most of the wrapper classes have built in compareTo() methods to save you from any thought. e.g., your comparator could just call Integer.compareTo(), i.e.

     return i1.compareTo(i2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: class Program { static void Main(string[] args) { new
import java.io.*; public class Demo{ public static void main(String[] args){ File f = new
I am really struggling with this question: import java.util.regex.*; class Regex2 { public static
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
I have the following program: ~/test> cat test.cc int main() { int i =
I have the following Programm package utests; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.ArrayList; import
A newbie question. I have the following piece of Java code: import acm.program.*; import
I have three programs, first does a selenium test import com.thoughtworks.selenium.*; import java.util.regex.Pattern; import
I have the following Python 2.6 program and YAML definition (using PyYAML ): import

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.