Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用自定义起点对ArrayList对象属性进行排序_Java_Sorting_Arraylist_Conditional Statements - Fatal编程技术网

Java 使用自定义起点对ArrayList对象属性进行排序

Java 使用自定义起点对ArrayList对象属性进行排序,java,sorting,arraylist,conditional-statements,Java,Sorting,Arraylist,Conditional Statements,我的问题是如何从自定义条件开始,按其中一个属性对具有自定义对象的ArrayList进行排序 让我解释清楚,这是我的代码: public static void sortArrayListByProperty(ArrayList colorList){ Collections.sort(colorList, new Comparator(){ public int compare(Object emp1, Object emp2){

我的问题是如何从自定义条件开始,按其中一个属性对具有自定义对象的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;    
            }
        });
    }
假设我希望数字从3开始,然后我需要

3 5 9 14 1
我希望是清楚的

可能吗

@约阿希姆·绍尔

谢谢你,我编辑了你的代码,改变了返回值,它成功了

编辑代码:

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;    
}
按15790320排序:

15790320
16448250
16777215
4013373

没有经过测试,但你知道了:你有两种情况

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

if(intValue1=start&&intValue2>=start){
如果(intValue1intValue2)
返回-1;
其他的
返回0;
}否则{
如果(intValue1<开始)
返回-1;
其他的
返回1;
}

未经测试,但您知道:您有两种情况

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

if(intValue1=start&&intValue2>=start){
如果(intValue1intValue2)
返回-1;
其他的
返回0;
}否则{
如果(intValue1<开始)
返回-1;
其他的
返回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;    
    }
  }
}
公共类ColorCounterComparator实现Comparator{
私人最终门槛;
公共ColorCounterComparator(最终整数阈值){
这个阈值=阈值;
}
@凌驾
公共整数比较(颜色计数器c1、颜色计数器c2){
int cv1=c1.getIntColorValue();
int cv2=c1.getIntColorValue();
如果(cv1>=阈值和&cv2<阈值){
返回-1;
}否则如果(cv2>=阈值和&cv2<阈值){
返回1;
}否则如果(cv1cv2){
返回1;
}否则{
返回0;
}
}
}
这显然是未经测试的,可能会出现一些一个接一个的错误,并且可能会翻转-1/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;    
    }
  }
}
公共类ColorCounterComparator实现Comparator{
私人最终门槛;
公共ColorCounterComparator(最终整数阈值){
这个阈值=阈值;
}
@凌驾
公共整数比较(颜色计数器c1、颜色计数器c2){
int cv1=c1.getIntColorValue();
int cv2=c1.getIntColorValue();
如果(cv1>=阈值和&cv2<阈值){
返回-1;
}否则如果(cv2>=阈值和&cv2<阈值){
返回1;
}否则如果(cv1cv2){
返回1;
}否则{
返回0;
}
}
}


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

使用ArrayList子列表方法创建子列表,然后对该子列表进行排序。

使用ArrayList子列表方法创建子列表,然后对该子列表进行排序。

有很多解决方案,但我建议您使用简单的模式,如混合过滤器和排序或混合过滤器和排序合并?为什么1会大于14?每一个小于3的数字会大于任何大于3的数字吗?两个小于3的数字如何比较?如果在列表中添加2、8、10,会发生什么?这些输入之间的逻辑区别是什么?我认为最好将它们添加到所需的位置许多解决方案,但我建议您使用简单的模式,如混合过滤器和排序或混合过滤器和排序合并?为什么1会大于14?每一个小于3的数字会大于任何大于3的数字吗?两个小于3的数字如何比较?如果在列表中添加2、8、10,会发生什么?这些输入之间的逻辑区别是什么?我认为你最好将它们添加到所需的位置,因为你的比较器将有降序。如果
cv1@GuillaumePolet:谢谢,我希望已经修好了。这是一个很好的提醒。但是我会避免直接使用这个结构,因为它有太多的陷阱(例如,不要尝试将它用于浮点数,尤其是当它们靠近时)。是的,比较器将有降序。如果
cv1@GuillaumePolet:谢谢,我希望已经修好了。这是一个很好的提醒。但我会避免直接使用该结构,因为它有太多的陷阱(例如,不要尝试将其用于浮点数,尤其是当它们靠近时)。我认为这实际上不起作用,因为假设你有
3511967210
要实际排序的数字不是连续的。@GuillaumePolet我不明白为什么这不起作用?您可以使用subList方法简单地将原始列表的一个范围作为视图获取。您可以对该范围进行排序—无论该范围内的数字是否连续。@FabianBarney根据我的理解,在我的示例中,如果说只有等于或大于3的值才应该排序,那么您应该有
359671012
。现在,如果你想对这样的列表进行排序,你会使用哪些子列表索引?@GuillaumePolet-Ah,明白了。OP希望通过值而不是索引来确定起点。完全误解了这个问题。Sry:)我不认为这真的会起作用,因为想象一下你有35 19 6 7 2 10的数字
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;
}
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;    
    }
  }
}