cn.nukkit.utils.HumanStringComparator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of powernukkit Show documentation
Show all versions of powernukkit Show documentation
A Minecraft Bedrock Edition server software implementation made in Java from scratch which supports all new features.
package cn.nukkit.utils;
import cn.nukkit.api.PowerNukkitOnly;
import cn.nukkit.api.Since;
import java.util.Comparator;
@PowerNukkitOnly
@Since("1.4.0.0-PN")
public class HumanStringComparator implements Comparator {
private static final HumanStringComparator INSTANCE = new HumanStringComparator();
@Override
public int compare(String o1, String o2) {
String o1StringPart = o1.replaceAll("\\d", "");
String o2StringPart = o2.replaceAll("\\d", "");
if (o1StringPart.equalsIgnoreCase(o2StringPart)) {
return extractInt(o1) - extractInt(o2);
}
return o1.compareTo(o2);
}
int extractInt(String s) {
String num = s.replaceAll("\\D", "");
// return 0 if no digits found
try {
return num.isEmpty() ? 0 : Integer.parseInt(num);
} catch (NumberFormatException e) {
return 0;
}
}
@PowerNukkitOnly
@Since("1.4.0.0-PN")
public static HumanStringComparator getInstance() {
return INSTANCE;
}
}