0

I am trying to sort VariantOptionData ArrayList using its one of parameter.

here is my VariantOptionData

public class VariantOptionData  implements java.io.Serializable 
{

   private Collection<VariantOptionQualifierData> variantOptionQualifiers;
   private StockData stock;
   private NGCPSizeEnum cs2;
}

And I want to sort this list using cs2 parameter. I am doing like this ..

List<VariantOptionData> allSizes = Converters.convertAll(style.getVariants(), getVariantOptionDataConverter());

Collections.sort(allSizes, new Comparator<VariantOptionData>()
{
        @Override
        public int compare(final VariantOptionData s1, final VariantOptionData s2)
        {
            return Integer.parseInt(s1.getCs2().getCode()) < Integer.parseInt(s1.getCs2().getCode()) ? s1.getCs2().getCode() : s2.getCs2().getCode();
        }
});

I know its wrong, but I am trying something like this but not able to understand how exactly Comparators are working. cs2 is a enum fields which having code as a increment value. I want to sort this list using Integer.parseInt(s1.getCs2().getCode()) increasing order. Please help

Free-Minded
  • 5,322
  • 6
  • 50
  • 93

4 Answers4

2

You have to return not the biggest value in comparator, but, for example substract them:

@Override
        public int compare(final VariantOptionData s1, final VariantOptionData s2)
        {
            return Integer.parseInt(s1.getCs2().getCode()) - Integer.parseInt(s2.getCs2().getCode());
        }
Heisenberg
  • 3,153
  • 3
  • 27
  • 55
1

If you look at the Javadoc for compare() you can find out how it is expected to work:

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

To follow this rule, all you need to do is a simple subtraction:

    @Override
    public int compare(final VariantOptionData s1, final VariantOptionData s2)
    {
        return Integer.parseInt(s1.getCs2().getCode()) - Integer.parseInt(s2.getCs2().getCode());
    }
Keppil
  • 45,603
  • 8
  • 97
  • 119
1

The reason why it is not working is that the compare method is wrong. You need to return the following: 0 if both elements are equal, a negative one if the first value is lower than the second, and a positive one if the first one is greater than the second.

@Override
public int compare(final VariantOptionData s1, final VariantOptionData s2) {
        return Integer.parseInt(s1.getCs2().getCode()) - Integer.parseInt(s2.getCs2().getCode());
}
Balduz
  • 3,560
  • 19
  • 35
0

Well, since we don't have some parts of your code, and we don't have a stacktrace neither, i'm going to try to help you with what i have.

Your code seems nice, you just need to return a number in compare method, and i just can see you need a getter method for the cs2 attribute in VariantOptionData, and it must have a "code" attribute too.

Comparator is an interface, that's why you are implementing the compare method while instanciating it, we call it "annonymous class". And the Collections.sort will sort it with the rule you implement inside the compare method.

Bruno Franco
  • 2,028
  • 11
  • 20