我是先做这样的:
List<String> items = new ArrayList<String>();
Comparator<items> ignoreLeadingThe = new Comparator<items>() {
public int compare(String a, String b) {
a = a.replaceAll("(?i(^the\\s+", "");
b = b.replaceAll("(?i(^the\\s+", "");
return a.compareToIgnoreCase(b);
}
};
Collections.sort(items, ignoreLeadingThe);
现在我正在这样做:
ItemObject[] io = new ItemObject[items.size()];
Comparator<ItemObject> ignoreLeadingThe = new Comparator<ItemObject>() {
public int compare(ItemObject a, ItemObject b) {
a.name = a.name.replaceAll("(?i(^the\\s+", "");
b.name = b.name.replaceAll("(?i(^the\\s+", "");
return a.name.compareToIgnoreCase(b.name);
}
};
Arrays.sort(io, ignoreLeadingThe);
当我对ArrayList
顶部进行排序时,它表现正常;它忽略了“The”并相应地排序了列表;但它实际上并没有影响列表的输出。
但是,当我对常规Array
(填充对象而不是字符串)进行排序时,底部代码实际上删除了“The”。例如,“小丑”将变成“小丑”。
有谁看到这里出了什么问题?