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

net.greypanther.natsort.SimpleNaturalComparator Maven / Gradle / Ivy

Go to download

Java implementation of the "natural sort" (also known as Alphanum sort) algorithm

The newest version!
package net.greypanther.natsort;

import java.util.Comparator;

/**
 * Compares Strings (or any other CharSequence subclass) using the
 * natural sort /
 * alphanum algorithm which gives a more
 * "natural" ordering when presenting the sorted list of strings to humans.
 * 
 * 

* This is a fast implementation of the original algorithm which produces no garbage during its run. * There is also a case-insensitive variant you might want to use: * {@link CaseInsensitiveSimpleNaturalComparator}. This is a fully self-contained implementation * compiled with Java 1.6 for maximum compatibility which does not add any additional dependencies * to your project. * *

* There are still limitations of this implementation to be aware of (which hopefully will be * addressed in future releases): * *

    *
  • Does not play nice with Unicode, especially characters which are outside of the BMP (ie. * codepoints with values larger than {@link Character#MAX_VALUE}).
  • *
  • Does not handle fractions or grouping characters properly.
  • *
  • Only understands integer values up to 2^64-1.
  • *
*/ public final class SimpleNaturalComparator extends AbstractSimpleNaturalComparator implements Comparator { @SuppressWarnings("rawtypes") private static final Comparator INSTANCE = new SimpleNaturalComparator(); private SimpleNaturalComparator() { // to be instantiated only internally } @Override int compareChars(char c1, char c2) { return c1 - c2; } @SuppressWarnings("unchecked") public static Comparator getInstance() { return INSTANCE; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy