sharkbait
sharkbait

Reputation: 3040

Comparator for list on a specific item's field

I have a List element.

Every OrderType item has an integer field, priority, that set the priority (1, 2 or 3) of the reassortment of the list.

I want create a comparator that reassort the list from priority 1 to 3. Can I use the Collections.sort method?

I saw this example:

package com.javacodegeeks.example;

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

public class SimpleSortExample {

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

        for (int i=0;i<10;i++) {
            list.add((int) (Math.random() * 100));
        }

        System.out.println("Initial List: "+list);
        Collections.sort(list);
        System.out.println("Sorted List: "+list);

    }

}

There are some examples here.

How can I do this?

I tried with:

Collections.sort(list, new Comparator() {

        @Override
        public int compare(Integer i1, Integer i2) {
            return (i2.intValue() > i1.intValue()) ? 1 : -1;
        }

    });

but I obtain an error, I have to implement the not ovveride method compare(object, Object)...

Like compare(Integer, Integer) is not an override method.

Upvotes: 0

Views: 2938

Answers (3)

glglgl
glglgl

Reputation: 91059

Either you have to use the proper List<> type such as List<Integer>, then using Integer should be fine. This is the adviseable alternative.

Or, if you insist on using the untyped List, you should do

    @Override
    public int compare(Object i1, Object i2) {
        ...
    }

instead. This second alternative is inferior to the other: you have less type safety and have to cast around for being able to use it.


For using a List<OrderType>, you have to completely change your code, as you cannot do

    for (int i=0;i<10;i++) {
        list.add((int) (Math.random() * 100));
    }

any longer (of course).

Additionally, the Comparator should be used like

    Collections.sort(l, new Comparator<OrderType>() {

        @Override
        public int compare(OrderType o1, OrderType o2) {
            return Integer.compare(oT1.getPriority(), oT2.getPriority());
        }
    });

Note especially

  • the Comparator<OrderType> part which makes compare() have the proper parameter types
  • the Integer.compare() call which makes the check much easier (the other examples fail to compare for equality...)

Upvotes: 3

QuakeCore
QuakeCore

Reputation: 1926

public class OrderTypeModel implements Comparable<OrderTypeModel> {

private Integer priority;
    @Override
    public int compareTo(OrderTypeModel o) {

        return priority.compareTo(o.priority);
    }
}

You can simply implement Comparable and call Collections.sort on your OrderTypeModel arrayList.

Upvotes: 3

RPresle
RPresle

Reputation: 2561

Use the second example from what you gave us

    Collections.sort(list, new Comparator() {

        @Override
        public int compare(OrderType oT1, OrderType oT2) {
            return (oT1.getPriority() > oT2.getPriority()) ? 1 : -1;
        }

    });

You give a comparator and just implement the compare method. This method takes two object of the (List< T >) T parameter. Then you can access a property from each object and compare them.

Consider setting the Generic type of your list of objects.

Upvotes: 2

Related Questions