How to customize order / sort objects in a list

I have a list of objects that I want to order / sort in a specific order.

I have a list of Promotion objects. Each promotion will have a description. Two of all promotions will have a description set to “premium” and “regular”

I want to order / sort the list so that the promotion with the description "premium" should always be at the end of the list, and the promotion with the description of the "ordinary" show should always be in list.size - 1 position. Example below

[{description = ...}, {description = ...}, ..... {description = regular}, {Description = premium}]

I tried using Collections.sort to sort objects by passing a custom Comparator as shown below

public int compare(Promotion p1, Promotion p2) {
        if(p1.getDescription().equals("ordinary")) {
    return -size-1; // size is the size of the list passed as a constructor argument to the Comparator.
    }
if(p1.getDescription().equals("premium")) {
    return -size;
}
return 0;
}

, .

if, , p2,

p1.getDescription().equals("premium") || p2.getDescription().equals("premium")

? Collections.sort ? - / ?

+6
3

Comparator compare , x < y, 0, x y , x > y.

, :

String classes[] = new String[]{"premimum", "ordinary", "less than ordinary"};

:

public int compare(Promotion p1, Promotion p2) {
Integer index1 = getIndexInsideClasses(p1.getDescription);
Integer index2 = getIndexInsideClasses(p2.getDescription);
       return index1.compareTo(index2);
   }
+6

:

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.

, , . :

        public int compare(Integer o1, Integer o2) {
            if(o1.intValue() < o2.intValue()) 
                return -1;
            if(o1.intValue() > o2.intValue()) 
                return 1;

            return 0;
        }

, , , , , , 0.

, - :

        public int compare(String o1, String o2) {
            if(o1.equals(o2))
                return 0; 
            if(o1.equals("ordinary") && o2.equals("premium")) 
                return 1;
            if(o1.equals("premium") && o2.equals("ordinary")) 
                return -1;

            return 0;
        }

, 0. "", -1, "", 1.

, . , , . - .

"" "", :

public int compare(String o1, String o2) {
    if(o1.equals(o2))
        return 0; 
    if(o1.equals("ordinary") || o2.equals("premium")) 
        return 1;
    if(o1.equals("premium") || o2.equals("ordinary")) 
        return -1;

    return 0;
}

, , o1 "", 1, , o2 "", -1, . "".

0

Alnours ( ):

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/** Note: this comparator imposes orderings that are inconsistent with equals. */
public class MyComparator implements Comparator<Promotion> {

    // any value not on the list will be considered less than the values on it
    // since indexOf returns -1 for these.
    private static final List<String> CLASSES = 
            Collections.unmodifiableList(Arrays.asList("ordinary", "premium"));

    @Override
    public int compare(Promotion p1, Promotion p2) {
        int i1 = CLASSES.indexOf(p1.getDescription());
        int i2 = CLASSES.indexOf(p2.getDescription());
        return Integer.compare(i1, i2);
    }
}

This will lead to the movement of all promotions with the description specified in CLASSESto the end of the order in accordance with the procedure specified in CLASSES. All promotions, the description of which is not on CLASSES, retain their relative order.

0
source

All Articles