Giter Site home page Giter Site logo

farbodsafaei / alphanumeric-comparator Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 3.0 53 KB

An alphanumeric comparator in Java to simplify comparison and sorting of strings in a more human readable format

License: Apache License 2.0

Java 100.00%
sorting comparator sorting-strings java sort alphanumeric-strings alphanumeric readability utility utilities utility-function

alphanumeric-comparator's Introduction

alphanumeric-comparator Build Status codecov Codacy Badge

An alphanumeric comparator in Java, for comparing and sorting strings in a more human readable format. It uses a combination of numeric and alphabetic comparisons to compare two strings. This class uses standard Java classes and is independent of any 3rd party libraries.

Additionally this comparator uses java.text.Collator class to correctly sort strings containing special characters like Umlauts or similar alphabet letters in different languages, such as: å, è, ü, ö, ø, or ý.

Example #1

For given list of strings:

file-01.doc, file-2.doc, file-03.doc

A regular lexicographical sort, e.g. java.util.Collections.sort(), sorts the above list in the following order:

file-01.doc, file-03.doc, file-2.doc

However using AlphaNumericComparator, the list will be sorted in a more meaningful and readable way:

file-01.doc, file-2.doc, file-03.doc

Example #2

For the following list of unsorted characters/strings:

b, e, k, ě, f, è, g

A regular lexicographical sort, e.g. java.util.Collections.sort(), sorts the above list in the following order:

b, e, f, g, k, è, ě

Using AlphaNumericComparator, the list is correctly sorted as below:

b, e, è, ě, f, g, k

Example #3

Consider the following unsorted list of words:

sèle, solo, solè, sola, soli, sole, sold, sila, silè, sölo, sulo, sylo, soly

Using Java Collections.sort() we will have:

sila, silè, sola, sold, sole, soli, solo, soly, solè, sulo, sylo, sèle, sölo

However with AlphaNumericComparator sort we will have a more accurate sorted list:

sèle, sila, silè, sola, sold, sole, solè, soli, solo, soly, sölo, sulo, sylo

How To

Simply pass the comparator as a parameter when sorting a list of strings:

List<String> list = new ArrayList<String>();
list.add("some string");
list.add("some other string");
...
list.sort(new AlphaNumericComparator());

or just simply use the compare() method in AlphaNumericComparator if needed to compare two strings.

alphanumeric-comparator's People

Contributors

farbodsafaei avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

alphanumeric-comparator's Issues

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.