All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.utils.HumanStringComparator Maven / Gradle / Ivy

Go to download

A Minecraft Bedrock Edition server software implementation made in Java from scratch which supports all new features.

There is a newer version: 1.6.0.1-PN
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy