英文:
Sorting strings of letters, numbers, and special characters
问题
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.Comparator;
class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("Bea", "hel", "Hel", "--'", "813", "!@#", "813", "813", "Alb");
Collections.sort(list, STANDARD_ALPHABETICAL_ORDER);
System.out.println(list);
}
public final static Comparator<String> STANDARD_ALPHABETICAL_ORDER =
(a,b) -> {
String newA = a.replaceAll("[^a-zA-Z0-9]", "");
String newB = b.replaceAll("[^a-zA-Z0-9]", "");
int na = newA.length();
int nb = newB.length();
int r;
int n;
if (na < nb) {
r = -1;
n = na;
} else if (na > nb) {
r = 1;
n = nb;
} else {
r = 0;
n = na;
}
for (int i = 0; i < n; ++i) {
char ca = newA.charAt(i);
char cb = newB.charAt(i);
if (ca != cb) {
if (Character.isDigit(ca) && !Character.isDigit(cb)) {
return -1;
} else if (!Character.isDigit(ca) && Character.isDigit(cb)) {
return 1;
} else if (ca > cb ) {
return 1;
} else {
return -1;
}
}
}
return r;
};
}
英文:
I have a list of strings that I would like to sort in a particular order.
The list is ["Bea", "hel", "Hel", "--'", "813", "!@#", "813", "813", "Alb"]
and I would like to have it sorted like:
[813, 813, 813, --', !@#, Alb, Bea, Hel, hel]
I have some code that gets pretty close but there is still one bug that I cannot figure out. Currently the program is producing this.
[813, 813, 813, Alb, --', !@#, Bea, Hel, hel]
Below is the code that I currently have
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;
import java.util.Comparator;
class Main {
public static void main(String[] args) {
List<String> list = Arrays.asList("Bea", "hel", "Hel", "--'", "813", "!@#", "813", "813", "Alb");
Collections.sort(list, STANDARD_ALPHABETICAL_ORDER);
System.out.println(list);
}
public final static Comparator<String> STANDARD_ALPHABETICAL_ORDER =
(a,b) -> {
String newA = a.replaceAll("[^a-zA-Z0-9]", "");
String newB = b.replaceAll("[^a-zA-Z0-9]", "");
int na = newA.length();
int nb = newB.length();
int r;
int n;
if (na < nb) {
r = -1;
n = na;
} else if (na > nb) {
r = -1;
n = nb;
} else {
r = 0;
n = na;
}
for (int i = 0; i < n; ++i) {
char ca = newA.charAt(i);
char cb = newB.charAt(i);
if (ca != cb) {
if (Character.isDigit(ca) && !Character.isDigit(cb)) {
return -1;
} else if (!Character.isDigit(ca) && Character.isDigit(cb)) {
return 1;
} else if (ca > cb ) {
return 1;
} else {
return -1;
}
}
}
return r;
};
}
答案1
得分: 0
这部分有错误:
} else if (na > nb) {
r = -1;
n = nb;
应该改为:
} else if (na > nb) {
r = 1;
n = nb;
这将使得--'
和!@#
在所有其他值之前出现,因为当替换已对变量newA
或newB
进行时,它们将成为空字符串。如果这不是预期的结果,请进行更改。
英文:
This part is buggy:
} else if (na > nb) {
r = -1;
n = nb;
Should be
} else if (na > nb) {
r = 1;
n = nb;
This would make appear --'
and !@#
before all other values, because they will be the empty string when the replacement has been done for the Variables newA
or newB
. You have to change this, if it is not the intended result.
专注分享java语言的经验与见解,让所有开发者获益!
评论