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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:30:22+00:00 2026-05-30T10:30:22+00:00

I am a C++ programmer and I am using Java at the moment (I

  • 0

I am a C++ programmer and I am using Java at the moment (I do have a considerable amount of java experience).

Basically, I want to recreate the pair<int,int> that I so commonly use in C++ and I want to have it sorted by the second integer value.

I am searching up on the internet and trying different ways of going about this, including using Comparator, Comparable etc.

I am basically creating a test program that looks like this:

import java.math.*;
import java.util.*;
import java.io.*;
import java.text.*;

class PairTest
{

    public static void main (String args[])  // entry point from OS
    {
        new PairTest().run();


    }

    public void run (){
        Pair foo = new Pair(1,2);
        System.out.println(foo.first + " "+ foo.second);
        ArrayList <Pair> al = new ArrayList<Pair>();
        for(int i =10;i>0;i--){
            al.add(new Pair(i, i*2));
        }
        for(int i =0;i<al.size();i++){
            System.out.println(al.get(i).first + " " + al.get(i).second);
        }
        Collections.sort(al);
        for(int i =0;i<al.size();i++){
            System.out.println(al.get(i).first + " " + al.get(i).second);
        }
    }

    private class Pair implements Comparable{

        public int first;
        public int second;

        public Pair (int a, int b){
            this.first = a;
            this.second = b;

        }

        int compareTo (Pair o){
            return new Integer(this.second).compareTo(new Integer(o.second));
        }
    }

}

What would be the best way to go about making a custom sorting function so the ArrayList sorts by the “second” variable. I want a quick and safe way of doing it, and at the moment, the compiler is telling me that “PairTest.Pair does not override abstract method compareTo…”

I really don’t know whats going on, any help would be greatly appreciated.

  • 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-30T10:30:23+00:00Added an answer on May 30, 2026 at 10:30 am

    There are two problems with your Pair class: it does not declare a generic parameter and the compareTo method needs to be public. Also, it is more efficient to just return the difference between int values than to construct Integer objects and invoke compareTo. Try this:

    private class Pair implements Comparable<Pair> {
    
        public int first;
        public int second;
    
        public Pair (int a, int b){
            this.first = a;
            this.second = b;
    
        }
    
        public int compareTo (Pair o){
            return second < o.second ? -1 : (second == o.second ? 0 : 1);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a novice programmer (I only have been using Java and Matlab for
I am a Java programmer and was working on a project using Hibernate and
I'm a C++ programmer thats considering using D for a personal project I want
I have a programmer who is using VB and LINQ; and I have a
I recall when I first read Pragmatic Programmer that they suggested using scripting languages
I am working with a programmer who is using Java to create an interface
So, I have this true/false question in my computer science class (we're using Java):
I am mainly a java programmer, using linux/os x for as long as i
I'm a C# programmer writing Java (for Android) and have a few technicalities of
I'm a Linux Python programmer with a (long) past Java experience I need a

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.