java list根据对象中String字段中的数字排序:
java list根据对象中String字段中的数字排序:
list.sort(new Comparator<Student>() {
@Override
public int compare(Student student, Student student1) {
return extractInt(student.getLocation()) - extractInt(student1.getLocation());
}
int extractInt(String s) {
String num = s.replaceAll("\\D", "");
return num.isEmpty() ? 0 : Integer.parseInt(num);
}
});