【问题标题】:Sort ArrayList Objects properties with custom start point使用自定义起点对 ArrayList 对象属性进行排序
【发布时间】:2012-05-10 04:26:26
【问题描述】:

我的问题是关于如何通过其中一个属性对具有自定义对象的 ArrayList 进行排序,但从自定义条件开始。

让我解释一下,这是我的代码:

public static void sortArrayListByProperty(ArrayList colorList){

        Collections.sort(colorList, new Comparator(){

            public int compare(Object emp1, Object emp2){

                int intValue1 = ((ColorCounter)emp1).getIntColorValue();        
                int intValue2 = ((ColorCounter)emp2).getIntColorValue();

                if(intValue1 < intValue2)
                    return 1;
                else if(intValue1 > intValue2)
                    return -1;
                else
                    return 0;    
            }
        });
    }

这会将我的 ArrayList 从大到小排序。

但我想要的是从我将指定的起始编号对我的 ArrayList 进行排序。

例如,如果 ArrayList 包含

5 3 9 1 14 

假设我希望数字从 3 开始,那么我需要有

3 5 9 14 1

我希望足够清楚......

有可能吗?

@Joachim Sauer

谢谢,我稍微修改了你的代码并更改了返回值,它成功了!

修改后的代码:

if (cv1 >= threshold && cv2 < threshold) {
   return -1;
} else if (cv2 >= threshold && cv2 < threshold) {
   return -1;
} else if (cv1 < cv2) {
   return 1;
} else if (cv1 > cv2) {
   return 1;
} else {
   return 0;    
}

测试示例:

16777215
16448250
15790320
4013373

按 15790320 排序:

15790320
16448250
16777215
4013373

【问题讨论】:

  • 许多解决方案,但我建议您使用简单的模式,例如混合过滤器和排序或混合过滤器和排序和合并?
  • 为什么 1 会大于 14?每个小于 3 的数都会大于任何大于 3 的数吗?两个小于 3 的数字如何相互比较?
  • 如果在此列表中添加 2、8、10 会发生什么?这些输入之间的逻辑差异是什么?我认为您最好将它们添加到所需的位置

标签: java sorting arraylist conditional-statements


【解决方案1】:

你可以试试这个:

public class ColorCounterComparator implements Comparator<ColorCounter> {
  private final threshold;

  public ColorCounterComparator(final int threshold) {
    this.threshold = threshold;
  }

  @Override
  public int compare (ColorCounter c1, ColorCounter c2) {
    int cv1 = c1.getIntColorValue();
    int cv2 = c1.getIntColorValue();

    if (cv1 >= threshold && cv2 < threshold) {
       return -1;
    } else if (cv2 >= threshold && cv2 < threshold) {
       return 1;
    } else if (cv1 < cv2) {
       return -1;
    } else if (cv1 > cv2) {
       return 1;
    } else {
       return 0;    
    }
  }
}

这显然是未经测试的,可能有一些错误,并且可能已经翻转了 -1/1 值。但它应该向您展示基本思想;-)

【讨论】:

  • 是的,您的比较器将按降序排列。如果cv1&lt;cv2 它应该返回-1。记住应该如何做的诀窍是简单地记住返回value1-value2;但否则很好的建议。
  • @GuillaumePolet:谢谢,已修复(我希望如此)。这是一个巧妙的提醒。但我会避免直接使用该构造,因为它有太多的陷阱(例如,不要尝试将它与浮点数一起使用,尤其是当它们靠近时)。
【解决方案2】:

使用ArrayList sublist 方法创建一个子列表,然后对这个子列表进行排序。

【讨论】:

  • 我认为这实际上行不通,因为假设您有3 5 1 9 6 7 2 10 实际排序的数字不是连续的。
  • @GuillaumePolet 我不明白为什么这不起作用?您可以使用 subList 方法简单地获取原始列表的范围作为视图。您可以对该范围进行排序 - 无论该范围内的数字是否连续。
  • @FabianBarney 据我了解,在我的示例中,只有等于或大于 3 的值才应该排序,你应该有 3 5 9 6 7 10 1 2。现在,如果你想对这样的列表进行排序,你会使用哪些子列表索引?
  • @GuillaumePolet 啊,明白了。 OP 想要一个按值而不是按索引的起点。完全误解了这个问题。对不起:)
【解决方案3】:

未经测试,但您明白了:您有两种情况

  • 两个数字都低于起始数字(在您的示例中为 3)或两者都高于 ==> 比较它们
  • 一个数字在起始编号下方,另一个在上方 ==> 第一个在您的自定义排序顺序中的第二个之后:

if (intValue1 < start && intValue2 < start || intValue1 >= start && intValue2 >= start) {
    if(intValue1 < intValue2)
        return 1;
    else if(intValue1 > intValue2)
        return -1;
    else
        return 0;    
} else {
    if (intValue1 < start)
        return -1;
    else
        return 1;
}

【讨论】:

    猜你喜欢
    • 2011-02-16
    • 1970-01-01
    相关资源
    最近更新 更多