1

ok I was going to edit my previous question but i wasnt sure if it was the right way to do it so i'll just give another question about Comparator, now i want to be able to sort with different ways. I have a bank checks and i want to sort with checkNumber then checkAmount i managed to do it with checkNumber but couldnt figure out how with checkAmount here is how i did it for checkNumber:

import java.util.Comparator;

public class Check implements Comparator {
    private int checkNumber;
    private String description;
    private double checkAmount;

    public Check() {


    }


    public Check(int newCheckNumber, double newAmountNumber) {

        setCheckNumber(newCheckNumber);
        setAmountNumber(newAmountNumber);
    }

    public String toString() {
        return  checkNumber + "\t\t" + checkAmount;
    }

    public void setCheckNumber(int checkNumber) {
        this.checkNumber = checkNumber;
    }

    public int getCheckNumber() {
        return checkNumber;
    }

    public void setAmountNumber(double amountNumber) {
        this.checkAmount = amountNumber;
    }

    public double getAmountNumber() {
        return checkAmount;
    }

    @Override
    public int compare(Object obj1, Object obj2) {

         int value1 = ((Check) obj1).getCheckNumber();
        int value2 = ((Check) obj2).getCheckNumber();

         int result = 0;

         if (value1 > value2){
             result = 1;
         }
         else if(value1 < value2){
             result = -1;
         }
         return result;
    }
}



import java.util.ArrayList;
import java.util.Collections;

import test.CheckValue;

public class TestCheck {

    public static void main(String[] args) {
        ArrayList List = new ArrayList();


        List.add(new Check(445, 55.0));
        List.add(new Check(101,43.12));
        List.add(new Check(110,101.0));
        List.add(new Check(553,300.21));
        List.add(new Check(123,32.1));


        Collections.sort(List, new Check());

        System.out.println("Check Number - Check Amount");

        for (int i = 0; i < List.size(); i++){

            System.out.println(List.get(i));
        }



    }

}

thank you very much in advance and please tell me if im submiting things in the wrong way.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
user319570
  • 45
  • 1
  • 1
  • 4
  • You could shorten your code and remove everything that's not related to your current problem. This way more people are willing to read it and you will get more help. – tangens May 16 '10 at 08:02

2 Answers2

2

You should make Check implements Comparable<Check>, but not itself implements Comparator.

A Comparable type defines the natural ordering for the type, and a Comparator for a type is usually not the type itself, and defines their own custom ordering of that type.

Related questions


Also, you shouldn't use raw type. You need to use parameterized generic types, Comparable<Check>, Comparator<Check>, List<Check>, etc.

Related questions


A String example

Let's take a look at what String has:

An example of using this is the following:

    List<String> list = new ArrayList<String>(
        Arrays.asList("A", "B", "C", "aa", "bb", "cc")
    );

    Collections.sort(list);
    System.out.println(list);
    // prints "[A, B, C, aa, bb, cc]"

    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
    System.out.println(list);
    // prints "[A, aa, B, bb, C, cc]"

Here's an example of sorting List<String> using both its natural ordering and your own custom Comparator<String>. Note that we've defined our own Comparator<String> without even changing the final class String itself.

    List<String> list = new ArrayList<String>(
        Arrays.asList("1", "000000", "22", "100")
    );
    Collections.sort(list);
    System.out.println(list);
    // prints "[000000, 1, 100, 22]" natural lexicographical ordering

    Comparator<String> lengthComparator = new Comparator<String>() {
        @Override public int compare(String s1, String s2) {
            return Integer.valueOf(s1.length())
                .compareTo(s2.length());
        }           
    };
    Collections.sort(list, lengthComparator);
    System.out.println(list);
    // prints "[1, 22, 100, 000000]" ordered by length

    Comparator<String> integerParseComparator = new Comparator<String>() {
        @Override public int compare(String s1, String s2) {
            return Integer.valueOf(Integer.parseInt(s1))
                .compareTo(Integer.parseInt(s2));
        }           
    };
    Collections.sort(list, integerParseComparator);
    System.out.println(list);
    // prints "[000000, 1, 22, 100]" ordered by their values as integers

Conclusion

You can follow the example set by String, and do something like this:

public class Check implements Comparable<Check> {

   public static final Comparator<Check> NUMBER_ORDER = ...
   public static final Comparator<Check> AMOUNT_ORDER = ...
   public static final Comparator<Check> SOMETHING_ELSE_ORDER = ...

}

Then you can sort a List<Check> as follows:

List<Check> checks = ...;
Collections.sort(checks, Check.AMOUNT_ORDER);
Community
  • 1
  • 1
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
2

What you really want to do is define a separate class to act as the Comparator object - don't make your actual Check class the comparator, but instead have 3 classes:

  1. the Check class itself
  2. a CheckAmountComparator class (or something similar) that implements Comparator<Check>
  3. a CheckNumberComparator class (or something similar) that implements Comparator<Check>

Then when you want to sort one way or another, you simply pass an instance of the Comparator-implementing class corresponding to the type of sorting you want to do. For instance, to sort by amount, it'd then become...

Collections.sort(yourListVariable, new CheckAmountComparator());

Also - I'd highly suggest naming your variable something other than List, since List is used as a type name in Java.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • you know what you really making sense! the instructor also suggested to use separate classes but i somehow ignored it :/ – user319570 May 16 '10 at 07:56
  • how would a do it for a type double which is AmountCheck though? – user319570 May 16 '10 at 07:58
  • Should be able to do it the same way you would for `int` except your `value1` and `value2` would need to be `double` instead of `int`. – Amber May 16 '10 at 08:06
  • i dont know how i did it but it worked lol. I added a method public double getCheckAmount() { return checkAmount; } in Check Class and it worked. could you explain why? – user319570 May 16 '10 at 08:20