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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:50:25+00:00 2026-06-10T08:50:25+00:00

I founded this code on the web . Excuse-me if a ask here,but I

  • 0

I founded this code on the web . Excuse-me if a ask here,but I haven’t only understand one thing. What does exactly return population2 – population1;?
It sorts the items by comparing every time each other (o1 and o2) , by taking the part after the colon without any eventual spaces (leadings and finals) , so the numbers, and sorting them by…?

import java.util.Comparator;
import java.util.PriorityQueue;
public class Main {
 public static void main(String[] args) {
  PriorityQueue<String> queue = new PriorityQueue<String>(11,
  new Comparator<String>() {
   public int compare(String o1, String o2) {
   int population1 = Integer.parseInt(o1.split(":")[1].trim());
   int population2 = Integer.parseInt(o2.split(":")[1].trim());
   return population2 - population1;
   }
  });
queue.add("United States: 307006550");
queue.add("Brazil: 193733800");
queue.offer("Russia: 141850000");
queue.offer("India: 1155347700");
queue.offer("China: 1331460000");
System.out.println("Countries in database: " + queue.size());
while (!queue.isEmpty()) {
  System.out.println(queue.poll());
}
System.out.println("Countries in database: " + queue.size());
 }
}

If i try to change, for example the code to:

return population1 - population2;

it orders the numbers as:

United States: 307006550
Russia: 141850000
Brazil: 193733800
India: 1155347700
China: 1331460000

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-06-10T08:50:26+00:00Added an answer on June 10, 2026 at 8:50 am

    The way comparisons are done is by comparing one value against another. However, because comparisons result in three possible answers instead of two, we need a convenient way to let the sorter know if the first element is less than, equal to, or greater than the second element. The easiest way to do this is to say that if element one is less than element two, return a negative number, if they are equal return a 0, and if the first element is greater, then return a positive number.

    We can write this manually in code as

    if(population1 < population2)
        return -1;
    else if(population1 > population2)
        return 1;
    return 0;
    

    However, the sorter is only checking for negative and positive values, so the magnitude of the result does not matter, so long as a negative number means less than and a positive number means greater than. Now, because the two values we are comparing are numbers, and we want to sort them in descending order, we can just do population1 - popultaion2. This will return a negative value if population1 is less than population2, 0 if they are the same, and a positive value if population1 is greater than population2. It is simply a convenience. If you want to opposite effect (sorting in ascending order), simply reverse the statement (as you have already shown).

    In regards to sleax’s latest comment, Java uses a modified Merge Sort that uses the information returned from the compareTo(..) function. You can read about how Merge Sort works at Wikipedia.

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

Sidebar

Related Questions

Now i have this code, but it always set null UNICODE_STRING str; char *cmp
In this code I want to change the string founded to integer and want
I've searched here in SO and all around but no properly solutions founded for
EDIT 23/11 : Solution founded ... I wrote $_db instead $this->_db in the constructor
i'm trying for days to get this working.. but i still fail. <?php $province
I Founded my problem in this post Updating ExpandableListView with notifyDataSetChanged() each time you
I have some class around this code, let call it ToolbarButton TBBUTTON tbbutton; ZeroMemory(&tbbutton,
I have a small problem but couldn't find solution for that. I have founded
I am dumb founded at this statement....maybe its just too many hours/days of doing
This was working, but now i'm getting the below error when calling update_attributes :

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.