sortFun(prop, order) { if (order) { order = 1; } else { order = -1; } let that = this; return function (a, b) { let res = 0; let char1 = a[prop]; let char1Type = that.getChartType(char1); let char2 = b[prop]; let char2Type = that.getChartType(char2); // 类型相同的逐个比较字符 if (char1Type[0] === char2Type[0]) { if (char1 === char2) { res = 0; } else { if (char1Type[0] === "zh") { res = char1.localeCompare(char2); } else if (char1Type[0] === "en") { let code1 = char1; let code2 = char2; if (char1.charCodeAt(0) < 97) { //大写字母 code1 = char1.toLowerCase(); } if (char2.charCodeAt(0) < 97) { code2 = char2.toLowerCase(); } res = code1.charCodeAt(0) - code2.charCodeAt(0); } else { res = char1 - char2; } } } else { // 类型不同的,直接用返回的数字相减 var num1 = char1Type[1]; var num2 = char2Type[1]; res = num1 - num2; } return res * order; }; } 转为java代码
时间: 2024-03-20 15:41:02 浏览: 13
以下是将该代码块转换为 Java 代码的示例:
```
public int sortFun(String prop, boolean order) {
if (order) {
order = true;
} else {
order = false;
}
YourClass that = this;
return (a, b) -> {
int res = 0;
String char1 = a.getProp(prop);
List<Object> char1Type = that.getChartType(char1);
String char2 = b.getProp(prop);
List<Object> char2Type = that.getChartType(char2);
if (char1Type.get(0).equals(char2Type.get(0))) {
if (char1.equals(char2)) {
res = 0;
} else {
if (char1Type.get(0).equals("zh")) {
res = char1.compareTo(char2);
} else if (char1Type.get(0).equals("en")) {
int code1 = char1.charAt(0);
int code2 = char2.charAt(0);
if (char1.charAt(0) < 97) {
code1 = Character.toLowerCase(char1.charAt(0));
}
if (char2.charAt(0) < 97) {
code2 = Character.toLowerCase(char2.charAt(0));
}
res = code1 - code2;
} else {
res = Integer.parseInt(char1) - Integer.parseInt(char2);
}
}
} else {
int num1 = Integer.parseInt(char1Type.get(1).toString());
int num2 = Integer.parseInt(char2Type.get(1).toString());
res = num1 - num2;
}
return res * (order ? 1 : -1);
};
}
```
注意,这里假设 `getChartType` 方法返回的是一个包含两个元素的列表,第一个元素为字符类型('zh'、'en' 或 'num'),第二个元素为字符对应的数字(如果是数字类型的字符)。如果该方法返回的类型不同,请适当修改 Java 代码。