Giter Site home page Giter Site logo

naturalsort's Introduction

NaturalSort

Perform natural order comparisons of strings in Java

sort

Most string comparators in programming languages compare strings based on the ASCII values of their individual characters. As an example, consider the following (unsorted) array of strings:

[“test2.txt”, “test10.txt”, “test1.txt”]

If we sort this array using the type an ASCII-value based comparator as described above, the result is the following:

[“test1.txt”, “test10.txt”, “test2.txt”]

However, if you were to create 3 files with these names in the same directory on your computer and viewed the contents of the directory sorted by name using a file manager application (such as the Windows Explorer or the Finder), you will notice that they are actually sorted as follows:

[“test1.txt”, “test2.txt”, “test10.txt”]

This ordering is more intuitive to end-users because 10 is bigger than 2.
A whole number is any sequence of numeric characters (0-9) that occur consecutively. So, "1 2 10" would come before "1 10 2" (each of these strings has 3 whole numbers and 2 is less than 10), but "1210" would come after "1102" (each of these strings has 1 whole number and 1102 is less than 1210). For the purposes of this problem, we will not consider any fractional numbers, so '.' and '/' should just be treated as delimiters like any other character. That is to say, "1/5" would come before "1/20" (each of these strings has 2 whole numbers that are compared and 5 is less than 20). We will also not consider negative numbers, meaning that ‘-‘ should be interpreted as a delimiter like any other character (“-1” would come before “-5” because ‘-‘ is equal in both strings and 1 is less than 5).

Useful Links

Interface Comparator
Java Comparator interface

naturalsort's People

Contributors

ritwik12 avatar

Stargazers

Nikunj Kumar Gupta avatar  avatar

Watchers

James Cloos avatar  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.