我需要对匹配项目中出现的数组进行排序,其他项目会出现故障。
对于Ex。我有一个数组: [z,asxdf,abasdf,abcasdf,b,bc,bcd,c] 我需要的是当我传递KeyWord假设“b”时,它应该对给定的数组进行排序,其中所有以 b .. 开头的字符串将首先出现在那之后休息。这将产生最终输出: [b,bc,bcd,z,c,..(休息)。]
如果可以在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,或任何其他如果它可以帮我解决这个问题)
答案 0 :(得分:10)
回答是否可以使用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));
答案 1 :(得分: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]
答案 2 :(得分: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]
答案 3 :(得分:1)
我还要补充一点,您可以在此Oracle教程http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html中阅读Comparator
和Comparable
。
答案 4 :(得分: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
}
}
答案 5 :(得分: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
}
答案 6 :(得分: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);
} }
}