如何在列表

时间:2017-07-10 11:44:43

标签: java sorting

我有一个对象列表,我想按特定顺序订购/排序。

我有一个Promotion对象列表。每个促销活动都有描述。所有促销中的两个将描述设置为" premium"和"普通"

我想对列表进行排序/排序,以便通过描述" premium"应始终位于列表的末尾,并通过说明"普通" show始终在list.size - 1位置。以下示例

  

[{description = ...},{description = ...},..... {description = ordinary},   {描述=溢价}]

我尝试使用Collections.sort通过传递下面的自定义Comparator对对象进行排序

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?任何人都可以建议/帮助我吗?

3 个答案:

答案 0 :(得分:6)

Comparator方法compare需要两个元素,如果 x&lt; <},则返回负值。 y ,如果 x等于y 则为0;如果 x&gt;则为正值ÿ

在这种情况下,您可以将类放在一个按照优先级排序的数组中:

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);
   }

答案 1 :(得分:0)

根据比较文件:

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.当第一个值为&#34; premium&#34;我们应该返回-1,当第一个是&#34;普通&#34;我们返回1.

无论如何,在这种情况下使用字符串来重新描述并不是一个好主意。这是因为使用字符串来表示代码中的可用数据可能会令人困惑,而字符串应该主要用于用户交互。你应该使用像枚举这样的东西。

修改

如果值可能不是&#34;普通&#34;或者&#34; premium&#34;,而不是我们可以将代码更改为:

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是&#34;普通&#34;我们返回1将其移动到集合的末尾,如果o2是&#34;普通&#34;我们返回-1将其移动到最后。我们对&#34; premium&#34;做了相反的事情。

答案 2 :(得分:0)

只是优化Alnours code(他的答案应保持为已接受的答案):

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);
    }
}

根据CLASSES中给出的顺序,这将移动所有促销活动,其中CLASSES上的说明到最终排序。所有描述不在CLASSES的促销活动都会保持其相对顺序。