使用多个比较器多次对字符串列表进行排序
Using multiple Comparator to sort List of Strings multiple times
我一直在尝试对字符串进行分组,忽略列表中存在的大小写,并尝试以区分大小写的方式再次对它们进行排序。
所以,如果我有如下输入:
"Abc","DEF","abc","dEf"
那么我的输出应该被排序两次,因为使用比较器的第一级排序应该是:
"Abc","abc","DEF","dEf"
第二级排序应该是:
"abc","Abc","dEf","DEF"
我的代码如下,只包含一个比较器。我们可以使用多个比较器或调整单个比较器来实现这样的排序吗?
public class ListSortIgnoreCaseComparator {
/**
* @param args
*/
public static void main(String[] args) {
List<String> input = new ArrayList<>();
input.add("Abc");
input.add("DEF");
input.add("abc");
input.add("dEf");
System.out.println("Input: "+input);
Collections.sort(input, new GroupIgnoreCase());
System.out.println("Output: "+input);
}
}
/**
*
* @author
*
*/
class GroupIgnoreCase implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
}
/**
*
* @author
*
*/
class SortIgnoreCase implements Comparator<String> {
@Override
public int compare(String s3, String s4) {
if(s3.equalsIgnoreCase(s4)){
return s3.compareTo(s4);
}
return 0;
}
}
输出如下:
Input: [Abc, DEF, abc, dEf]
Output: [Abc, abc, DEF, dEf]
我们能否调整比较器,使输出为:
Output: [abc, Abc, dEf, DEF]
而不是上面的?
是的,可以不区分大小写比较,只有相等的时候再比较,这次不区分大小写:
@Override
public int compare(String s1, String s2) {
int cmp = s1.compareToIgnoreCase(s2);
if (cmp == 0)
cmp = s1.compareTo(s2);
return cmp;
}
这是我的解决方案:
@Override
public int compare(String s1, String s2) {
int comVal = s1.compareToIgnoreCase(s2);
if (comVal == 0) {
return s2.compareTo(s1);
} else {
return comVal;
}
}
Input: [Abc, DEF, abc, dEf]
Output: [abc, Abc, dEf, DEF]
使用 Collator
with strength TERTIARY
:
public class ListSortIgnoreCaseComparator {
/**
* @param args
*/
public static void main(String[] args) {
List<String> input = new ArrayList<>();
input.add("Abc");
input.add("DEF");
input.add("abc");
input.add("dEf");
System.out.println("Input: "+input);
Collator c = Collator.getInstance();
c.setStrength(Collator.TERTIARY);
Collections.sort(input, c);
System.out.println("Output: "+input);
}
}
进一步阅读:Comparing Strings (The Java™ Tutorials > Internationalization > Working with Text)
我一直在尝试对字符串进行分组,忽略列表中存在的大小写,并尝试以区分大小写的方式再次对它们进行排序。
所以,如果我有如下输入:
"Abc","DEF","abc","dEf"
那么我的输出应该被排序两次,因为使用比较器的第一级排序应该是:
"Abc","abc","DEF","dEf"
第二级排序应该是:
"abc","Abc","dEf","DEF"
我的代码如下,只包含一个比较器。我们可以使用多个比较器或调整单个比较器来实现这样的排序吗?
public class ListSortIgnoreCaseComparator {
/**
* @param args
*/
public static void main(String[] args) {
List<String> input = new ArrayList<>();
input.add("Abc");
input.add("DEF");
input.add("abc");
input.add("dEf");
System.out.println("Input: "+input);
Collections.sort(input, new GroupIgnoreCase());
System.out.println("Output: "+input);
}
}
/**
*
* @author
*
*/
class GroupIgnoreCase implements Comparator<String> {
@Override
public int compare(String s1, String s2) {
return s1.compareToIgnoreCase(s2);
}
}
/**
*
* @author
*
*/
class SortIgnoreCase implements Comparator<String> {
@Override
public int compare(String s3, String s4) {
if(s3.equalsIgnoreCase(s4)){
return s3.compareTo(s4);
}
return 0;
}
}
输出如下:
Input: [Abc, DEF, abc, dEf]
Output: [Abc, abc, DEF, dEf]
我们能否调整比较器,使输出为:
Output: [abc, Abc, dEf, DEF]
而不是上面的?
是的,可以不区分大小写比较,只有相等的时候再比较,这次不区分大小写:
@Override
public int compare(String s1, String s2) {
int cmp = s1.compareToIgnoreCase(s2);
if (cmp == 0)
cmp = s1.compareTo(s2);
return cmp;
}
这是我的解决方案:
@Override
public int compare(String s1, String s2) {
int comVal = s1.compareToIgnoreCase(s2);
if (comVal == 0) {
return s2.compareTo(s1);
} else {
return comVal;
}
}
Input: [Abc, DEF, abc, dEf]
Output: [abc, Abc, dEf, DEF]
使用 Collator
with strength TERTIARY
:
public class ListSortIgnoreCaseComparator {
/**
* @param args
*/
public static void main(String[] args) {
List<String> input = new ArrayList<>();
input.add("Abc");
input.add("DEF");
input.add("abc");
input.add("dEf");
System.out.println("Input: "+input);
Collator c = Collator.getInstance();
c.setStrength(Collator.TERTIARY);
Collections.sort(input, c);
System.out.println("Output: "+input);
}
}
进一步阅读:Comparing Strings (The Java™ Tutorials > Internationalization > Working with Text)