简体   繁体   English

如何使用比较器对数组中的某些特定元素进行排序

[英]How to sort some specific element in an array using comparator

I am trying to sort an array in a specific order. 我试图按特定顺序对数组进行排序。 if number % 10 is equal 0 then it will come first and for the rest, if number mod 10 is greater it will come first. 如果数字%10等于0,则它将排在最前面;如果数字mod 10大于10,它将排在最前面。

Collections.sort(arr,new Comparator<Integer>(){
   public int compare(Integer e1, Integer e2) {
       if(e1 %10 == 0 && e2 %10 != 0  || (e2 %10 == 0 && e1 %10 != 0)  
       || e1 % 10 ==0 || e2 % 10 ==0){
           return -1;
       }else{
           if(e1 % 10 < e2 % 10){
               return 1 ;
           }else{
               return -1 ;
           }
       }
   }});

input : [120, 20, 35, 7, 29] 输入:[120,20,35,7,29]

output: [29, 7, 35, 20, 120] 输出:[29、7、35、20、120]

expected: [120, 20, 29, 7 , 35] 预期:[120,20,29,7,35]

import java.util.*; 
import java.lang.*; 
import java.io.*; 
public class Main
{
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(120);
        list.add(20);
        list.add(35);
        list.add(7);
        list.add(29);

        Collections.sort(list,new Comparator<Integer>(){
           public int compare(Integer e1, Integer e2) {
               if(e1 %10 == 0  || e2%10 == 0){
                   return 1;
               }
               else {
                   if(e1 % 10 > e2 % 10){
                       return -1 ;
                   }else{
                       return 0 ;
                   }
               }
           }});

           Iterator<Integer> myListIterator = list.iterator(); 
           while(myListIterator.hasNext()) {
               System.out.println(myListIterator.next());
           }
    }
}

A comparator which return 0 indicates that the values are equal and nothing need to be switched. 返回0的比较器表示值相等,无需进行任何切换。 In your case, we have the say in the compare method, that the first if should return 0. This will give you the wanted result. 在您的情况下,compare方法中有说,第一个if应该返回0。这将为您提供所需的结果。

Collections.sort(arr, new Comparator<Integer>() {
    public int compare(Integer e1, Integer e2) {
        if (e1 % 10 == 0 && e2 % 10 != 0 || (e2 % 10 == 0 && e1 % 10 != 0) || e1 % 10 == 0 || e2 % 10 == 0) {
            return 0;
        } else {
            if (e1 % 10 < e2 % 10) {
                return 1;
            } else {
                return -1;
            }
        }
    }
});

The simplest way to do this with an explicit comparator is: 使用显式比较器执行此操作的最简单方法是:

public int compareTo(Integer a, Integer b) {
  int cmp = Boolean.compare(a % 10 != 0, b % 10 != 0);
  if (cmp != 0) {
    return cmp;
  }
  return a.compareTo(b);
}

Using Boolean.compare(a % 10 != 0, b % 10 != 0) finds if the last digit is zero or not: if a does not end with zero but b does, a positive value is returned, so a will be sorted after b . 使用Boolean.compare(a % 10 != 0, b % 10 != 0)查找最后一位是否为零:如果a不以零结尾但b则返回正值,因此a将为在b之后排序。

If the either both end with a zero, or neither ends with a zero, the numbers are compared naturally. 如果两端都为零,或者两端都不为零,则将自然比较这些数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM