Jimmy Huch
Jimmy Huch

Reputation: 4540

Best way for custom sorting in Java?

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.

Upvotes: 1

Views: 553

Answers (3)

PC.
PC.

Reputation: 7024

Override comapreTo method in your Pair class. No need to implement anything.

comapreTo method accepts Object as the argument

public int compareTo(Object another)
{
    return new Integer(this.second).compareTo(new Integer(((Pair)another).second));
}

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234795

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);
    }
}

Upvotes: 5

hqt
hqt

Reputation: 30276

In your code, you should change:

private class Pair implements Comparable

to

private class Pair implements Comparable<Pair>

And you change this line:

int compareTo (Pair o)

to

public int compareTo (Pair o)

because this function will be use outside of this class :)

That's all you need :)

Upvotes: 2

Related Questions