Java 通过忽略开头的-字符(如果存在)对arraylist排序
我想对包含许多行的arraylist进行排序 我想忽略'-' 如果是该行的第一个字母Java 通过忽略开头的-字符(如果存在)对arraylist排序,java,sorting,arraylist,Java,Sorting,Arraylist,我想对包含许多行的arraylist进行排序 我想忽略'-' 如果是该行的第一个字母 Collections.sort(arrList, new Comparator<String>() { @Override public int compare(String o1, String o2) { if (o1.startsWith("-") && o2.startsWith("-")) { return compare(o1.substrin
Collections.sort(arrList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1.startsWith("-") && o2.startsWith("-")) {
return compare(o1.substring(1), o2.substring(1));
}
if (o1.startsWith("-")) {
return 1;
}
if (o2.startsWith("-")) {
return -1;
}
return o1.compareTo(o2);
}
输入
-bbb
a-aaa
-ddd
b-ddd
c-ccc
输出
应该是(正如你所看到的,它应该按第一个字符(a b c d)排序)
a-aaa
-bbb
c-ccc
-ddd
e-eee
这是我的代码
Collections.sort(arrList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1.startsWith("-") && o2.startsWith("-")) {
return compare(o1.substring(1), o2.substring(1));
}
if (o1.startsWith("-")) {
return 1;
}
if (o2.startsWith("-")) {
return -1;
}
return o1.compareTo(o2);
}
Collections.sort(arrList,newcomparator(){
@凌驾
公共整数比较(字符串o1、字符串o2){
if(o1.startsWith(“-”)&o2.startsWith(“-”){
返回比较(o1.子串(1),o2.子串(1));
}
如果(o1.以(“-”)开头){
返回1;
}
如果(o2.以(“-”)开头){
返回-1;
}
返回o1。与(o2)相比;
}
}))
或者,简短但可读性较差
int o1offset = o1.startsWith("|") ? 1 : 0;
int o2offset = o2.startsWith("|") ? 1 : 0;
return o1.substring(o1offset).compareTo(o2.substring(o2offset));
在比较器中使用|
,而不是-
您的比较不正确,如果o1
以-
开头,则不能忽略o2
:
if (o1.startsWith("-")) {
return 1;
}
这将忽略许多结果
要正确比较,您必须遵循以下行为:如果
字符串
以-
开头,则获取第二个字符(位置1),否则获取第一个字符(位置0)。只要您不能将与
基本类型进行比较,则单个字符的子字符串将更有用
分别对每个字符串执行此操作,并对结果进行比较
知道了这一点,您可以实现您的比较器,如(还有其他简化和OneLiner答案,但为了让OP更好地理解,我将是基本的):
老实说,没有必要做这么多工作。只需在使用正则表达式时比较两个值。您最初遇到的问题是,当它们以-
开头时,您从未忽略它们,如果它以-
开头,您只会使一个lexograhm明显大于另一个
public static void main(String[] args) {
List<String> testList = new ArrayList<>();
testList.add("-bbb");
testList.add("a-aaa");
testList.add("-ddd");
testList.add("b-ddd");
testList.add("c-ccc");
Collections.sort(testList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// This removes leading - from the input and ignores them
// At the moment it removes one ore more -, if it should just be one
// remove the +
return o1.replaceAll("^-+", "").compareTo(o2.replaceAll("^-+", ""));
}
});
for(String s : testList) {
System.out.println(s);
}
}
以下是您需求的逻辑
public int compare(String str1, String str2) {
if(str1.startsWith("-")){
if(str2.startsWith("-")){
str1 = str1.substring(1);
str2 = str2.substring(1);
return str1.compareTo(str2);
}else{
str1 = str1.substring(1);
return str1.compareTo(str2);
}
} else if(str2.startsWith("-")){
if(str1.startsWith("-")){
str1 = str1.substring(1);
str2 = str2.substring(1);
return str1.compareTo(str2);
}else{
str2 = str2.substring(1);
return str1.compareTo(str2);
}
}else{
return str1.compareTo(str2);
}
}
在您的描述中,您使用了-
,但您的代码使用了|
-这应该是如何工作的?您也会立即使一个词法符号比另一个词法符号大,只要它以|
开头,但以后再也不关心词法符号值。您的问题是如何忽略它们,您的代码告诉我们不同。@JordiCastilla oh yeah,只是在那里写了随机值,并将它们替换为OP在问题中使用的值,但没有更改。添加输入。感谢您的提示这是可笑的过度复杂。很高兴因为被接受而为错误的答案感到自豪…通过-
更正符号以帮助fu真正的用户会比发布这条愚蠢的评论更有用(更快)…顺便说一句:downvote是我的,还在等待你的编辑被删除。。。
a-aaa
b-ddd
-bbb
c-ccc
-ddd
public static void main(String[] args) {
List<String> testList = new ArrayList<>();
testList.add("-bbb");
testList.add("a-aaa");
testList.add("-ddd");
testList.add("b-ddd");
testList.add("c-ccc");
Collections.sort(testList, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
// This removes leading - from the input and ignores them
// At the moment it removes one ore more -, if it should just be one
// remove the +
return o1.replaceAll("^-+", "").compareTo(o2.replaceAll("^-+", ""));
}
});
for(String s : testList) {
System.out.println(s);
}
}
a-aaa
b-ddd
-bbb
c-ccc
-ddd
public int compare(String str1, String str2) {
if(str1.startsWith("-")){
if(str2.startsWith("-")){
str1 = str1.substring(1);
str2 = str2.substring(1);
return str1.compareTo(str2);
}else{
str1 = str1.substring(1);
return str1.compareTo(str2);
}
} else if(str2.startsWith("-")){
if(str1.startsWith("-")){
str1 = str1.substring(1);
str2 = str2.substring(1);
return str1.compareTo(str2);
}else{
str2 = str2.substring(1);
return str1.compareTo(str2);
}
}else{
return str1.compareTo(str2);
}
}