7

我需要对一个数组进行排序,其中匹配项出现而其他项将下降。

对于前。我有一个数组:[ z , asxdf , abasdf , abcasdf , b , bc , bcd , c ] 我需要的是当我传递一个关键字假设"b"时,它应该对给定的数组进行排序,其中所有字符串都以b..将首先出现,其余的在此之后。这将产生最终输出:[ b , bc , bcd , z , c , .. (rest).]

如果可以在 Java 中使用比较器?

String keyWord = "b";
String[] s = {"z", "asxdf", "abasdf", "abcasdf", "b", "bc", "bcd", "c"};
Arrays.sort(s, new Comparator<String>() {

    @Override
    public int compare(String o1, String o2) {

            //Code to sort array according to need

    }
});
System.out.println(Arrays.toString(s));

Result -> [ b , bc , bcd , z , c , ...]

(如果可以帮助我解决这个问题,我可以使用 List 而不是 Array 或任何其他)

4

7 回答 7

11

回答是否可以使用 Comparator 的问题,答案是肯定的:您只需要创建一个新的 Comparator 类而不是创建一个匿名 Comparator,如下所示:

    class MyComparator implements Comparator<String> {

        private final String keyWord;

        MyComparator(String keyWord) {
            this.keyWord = keyWord;
        }

        @Override
        public int compare(String o1, String o2) {

            if(o1.startsWith(keyWord)) {
                return o2.startsWith(keyWord)? o1.compareTo(o2): -1;
            } else {
                return o2.startsWith(keyWord)? 1: o1.compareTo(o2); 
            }
        }
    }

然后在您的代码中使用该比较器:

    String keyWord = "b";
    String[] s = {"z", "asxdf", "abasdf", "abcasdf", "b", "bc", "bcd", "c"};
    Arrays.sort(s, new MyComparator(keyWord));
    System.out.println(Arrays.toString(s));
于 2013-06-25T06:19:42.303 回答
3

因为关键字匹配后的字符串长度似乎很重要,所以这是我的版本:

Arrays.sort(s, new Comparator<String>() {

    @Override
    public int compare(String o1, String o2) {

        boolean o1Has = o1.startsWith( keyWord );
        boolean o2Has = o2.startsWith( keyWord );

        if( o1Has && !o2Has  ) return -1;
        else if( o2Has && !o1Has ) return 1;
        else if( o1Has && o2Has ) return 0;
        else
            return o1.length() - o2.length();
    }
});

产量:

[b、bc、bcd、z、c、asxdf、abasdf、abcasdf]

于 2013-06-25T06:42:19.623 回答
1
final String keyWord = "b";
String[] s = {"z", "asxdf", "abasdf", "abcasdf", "b", "bc", "bcd", "c"};
Arrays.sort(s, new Comparator<String>() {

    @Override
    public int compare(String o1, String o2) {

            boolean o1_has_keyWord = o1.indexOf(keyWord.charAt(0)) == 0 && o1.contains(keyWord);
            boolean o2_has_keyWord = o2.indexOf(keyWord.charAt(0)) == 0 && o2.contains(keyWord);

        if (o1_has_keyWord && o2_has_keyWord)
        {
            if (o1.length() == o2.length())
            {
                if (o1.indexOf(keyWord.charAt(0)) > o2.indexOf(keyWord.charAt(0))){
                    return -1;
                }
                else if (o1.indexOf(keyWord.charAt(0)) == o2.indexOf(keyWord.charAt(0))){
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            else if (o1.length() > o2.length())
            {
                    return 1;
            }
            else 
            {
                return -1;
            }
        }
        else if (o1_has_keyWord && !o2_has_keyWord)
        {
            return -1;
        }
        else if (!o1_has_keyWord && o2_has_keyWord)
        {
            return 1;
        }


        return 0;

            //Code to sort array according to need

    }
});
System.out.println(Arrays.toString(s));

输出:

[b, bc, bcd, z, asxdf, abasdf, abcasdf, c]
于 2013-06-25T06:14:32.080 回答
1

我还要补充一点,您可以在此 Oracle 教程http://docs.oracle.com/javase/tutorial/collections/interfaces/order.htmlComparator中阅读有关内容。Comparable

于 2013-06-25T06:59:28.943 回答
0

关于什么:

List result = new ArrayList();
for(int i=0;i<s.length();i++) {
    if(s[i].startsWith(keyWord)) {
        result.add(s[i], 0);  // Adding to the beginning
    } else {
        result.add(s[i]);  // Adding to the end
    }
}
于 2013-06-25T06:13:56.367 回答
0

只是因为它更短:

@Override
public int compare(String o1, String o2) {
    boolean firstMatches = o1.startsWith(keyWord);
    boolean secondMatches = o2.startsWith(keyWord);
    if (firstMatches != secondMatches) {
        return firstMatches ? -1 : 1;
    }
    return 0;  // or refine the sort sort here
}
于 2013-06-25T06:26:40.363 回答
0

请检查以下

package com.pantech.sam.demos;

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

public class SortArray {

/**
 * @param args
 */
private final static char keyWord = 'b';

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String[] s = {"z", "asxdf", "abasdf", "abcasdf", "b", "bc", "bcd", "c"};

    System.out.println("FIRST WAY " + sortArray(s));

    Arrays.sort(s, new CustomeComparator(keyWord));
    System.out.println("SECOND WAY" + Arrays.toString(s));

}

static List<String> sortArray(String[] sArray){
    Arrays.sort(sArray) ;
    List<String> l1 = Arrays.asList(sArray);
    List<String> l2 = new ArrayList<String>(l1.size());
    int index = 0;
    for (String str : l1) {
        if (str.charAt(0) == keyWord) {
            l2.add(index++,str); // add at start
        }else{
            l2.add(l2.size(), str); // add at end
        }

    }
    return l2;
}

}


class CustomeComparator implements Comparator<String> {
private final char keyWord;

CustomeComparator(char keyword2) {
    this.keyWord = keyword2;
}


@Override
public int compare(String o1, String o2) {
    // TODO Auto-generated method stub
      if(o1.charAt(0) == (keyWord)) {
            return o2.charAt(0) == (keyWord)? o1.compareTo(o2): -1;
        } else {
            return o2.charAt(0) == (keyWord)? 1: o1.compareTo(o2); 
        }   }
}
于 2013-06-25T07:16:36.007 回答