2

Sorry, I don't work with Java since a long time. I have a class which sort int type, and I am trying to do this, but this does not compile:


MySort.sort(vectorOfInt, new Comparator() {
    @Override
    public int compare(Object o1, Object o2) {
    return (int)o1 - (int)o2;
    }
    });

I would like to sort a vector of int using this method using the comparator to sort in ascending order.


public static  void sort(T[] a, Comparator c) {
        sort(a, 0, a.length, c);
}

3
  • can you explain crescent order? Commented May 3, 2011 at 17:42
  • crescent = ascending. Which is: 1,2,3,4,5,6,7... Commented May 3, 2011 at 17:44
  • cannot convert from object to int Commented May 3, 2011 at 17:46

3 Answers 3

3

The sample code is rather incomplete, but to begin with you cannot cast Object to int. Use

return (Integer) o1 - (Integer) o2;

instead.

Sign up to request clarification or add additional context in comments.

2 Comments

I updated the code. I don't understand much about the first parameter. Could you help me? Thanks.
@Seva, the first parameter T[] a is an array of the generic type T, i.e. the class is defined as MySort<T>.
1

First off, don't use Vector, or Hashtable for that matter, they both are considered deprecated for all practical purposes. Use List or Map implementations.

Use a List<Integer> instead, it is the modern and correct way to manipulate a list of Integers as objects.

final List<Integer> list = new ArrayList<Integer>();
... populate list ...
Collections.sort(list);

If you want to use a different Comparator then you can use:

Collections.sort(list, comparator);

Since Integer implements Comparable, this will by default sort the List<Integer> in ascending order. Integer for the details.

This is the correct, idiomatic way of sorting List and Collection classes in modern Java. Anything else shows you don't understand the language and what it provides.

6 Comments

thanks, but in my case, I already have a sorting class. I updated the code.
you are doing it wrong then, you should never re-implement code that is already in the standard library.
@Jarrod, I'm not sure what you were trying to achieve, but your backticks near "List or Map" are producing formatting weirdness.
this code is a sorting algorithm which is not currently implemented in the Java 6. It will be implemented in Java 7.
If you are using a Comparator it is already in Java 6. Comparator and 'Comparable` has been in the language since the beginning. If you want to sort Integer then it is already built in. I don't see anything ground breaking in your code example that is new or enhanced over what Integer already does.
|
1

You can make a typed Comparator and you won't need to cast the arguments.

MySort.sort(vectorOfInt, new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
      return o1 - o2;
    }
    });

EDIT:
First off, please don't call an array a vector. Vector is a java class and people will assume that is what you are using when you name the variable vectorOfInt.

The compiler error you are getting is because the compiler doesn't know that your Comparator is operating on Integers. Since all it sees is an Object, it doesn't know that o1 is actually an Integer and can be unboxed to be an int. By specifying the comparator type, you provide more information and the conversion can be made implicitly.

3 Comments

I updated the code. I don't understand much about the first parameter. Could you help me? Thanks.
thanks about the vector teach. But I still can't compile, because the sort method expects a T[] a parameter.
@Seva: Where is T defined? It isn't a generic method, so it has to be set by the containing class. What is the class signature that contains sort()?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.