Coder Social home page Coder Social logo

cybernetics / alphanumeric-comparator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sawano/alphanumeric-comparator

0.0 2.0 0.0 803 KB

An implementation of a comparator that sort strings in an order that makes sense for a human.

License: Apache License 2.0

Java 100.00%

alphanumeric-comparator's Introduction

Build Status Maven Central

Alphanumeric Comparator

An implementation of a comparator that sort strings in an order that makes sense for a human. I.e. it performs an "alphabetical"/"alphanumeric" comparison instead of a lexicographical comparison.
It is mainly intended to be used to sort information that are to be read by humans (in UI's etc). So keep in mind that what the "correct" ordering should be is entirely subjective and application specific. It should however be straightforward to modify the comparator to suit any specific needs.

Examples

Sorting files

Given file names:

image-1.jpg
image-4.jpg
image-10.jpg
image-02.jpg
image-22.jpg
image-9.jpg
image-11.jpg

A standard lexicographical sort would produce the following:

image-02.jpg
image-1.jpg
image-10.jpg
image-11.jpg
image-22.jpg
image-4.jpg
image-9.jpg

But as a human, the numbers in the file name has a meaning an the file "9" should come before the file "10", and "02" should not come before "1". Using the AlphanumericComparator would produce the following result instead:

image-1.jpg
image-02.jpg
image-4.jpg
image-9.jpg
image-10.jpg
image-11.jpg
image-22.jpg

Another example

If we sort the strings:

Hard drive 20GB
Hard drive 2GB

A standard lexicographical sort would produce the following:

Hard drive 20GB
Hard drive 2GB

While the AlphanumericComparator would produce:

Hard drive 2GB
Hard drive 20GB

This comparator also supports the use of a locale-sensitive Collator for string comparison. This makes this comparator very flexible for sorting.

Code example

    @Test
    public void example() {
        final List<String> stringsToSort = asList("HD 20GB", "HD 2GB");

        stringsToSort.sort(new AlphanumericComparator(Locale.ENGLISH));
        
        assertEquals(asList("HD 2GB", "HD 20GB"), stringsToSort);
    }

And an example of how to sort using language specific collation rules:

    @Test
    public void should_demonstrate_sorting_with_swedish_collation() {
        final List<String> stringsToSort = asList("Ö", "Ø", "O");

        stringsToSort.sort(new AlphanumericComparator(new Locale("sv", "SE")));

        assertEquals(asList("O", "Ö", "Ø"), stringsToSort);
    }

Download

Releases are available at the Maven central repository. Or you can just use the source code directly if you prefer that.

Maven

<dependency>
    <groupId>se.sawano.java</groupId>
    <artifactId>alphanumeric-comparator</artifactId>
    <version>1.4.1</version>
</dependency>

Gradle

'se.sawano.java:alphanumeric-comparator:1.4.1'

alphanumeric-comparator's People

Contributors

kisty avatar sawano avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.