natural-compare

WebJar for natural-compare

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

natural-compare
Last Version

Last Version

1.4.0
Release Date

Release Date

Type

Type

jar
Description

Description

natural-compare
WebJar for natural-compare
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/litejs/natural-compare-lite

Download natural-compare

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/natural-compare/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>natural-compare</artifactId>
    <version>1.4.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/natural-compare/
implementation 'org.webjars.npm:natural-compare:1.4.0'
// https://jarcasting.com/artifacts/org.webjars.npm/natural-compare/
implementation ("org.webjars.npm:natural-compare:1.4.0")
'org.webjars.npm:natural-compare:jar:1.4.0'
<dependency org="org.webjars.npm" name="natural-compare" rev="1.4.0">
  <artifact name="natural-compare" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='natural-compare', version='1.4.0')
)
libraryDependencies += "org.webjars.npm" % "natural-compare" % "1.4.0"
[org.webjars.npm/natural-compare "1.4.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.
@version    1.4.0
@date       2015-10-26
@stability  3 - Stable

Natural Compare – Build Coverage

Compare strings containing a mix of letters and numbers in the way a human being would in sort order. This is described as a "natural ordering".

Standard sorting:   Natural order sorting:
    img1.png            img1.png
    img10.png           img2.png
    img12.png           img10.png
    img2.png            img12.png

String.naturalCompare returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order. Use it with builtin sort() function.

Installation

  • In browser
<script src=min.natural-compare.js></script>
  • In node.js: npm install natural-compare-lite
require("natural-compare-lite")

Usage

// Simple case sensitive example
var a = ["z1.doc", "z10.doc", "z17.doc", "z2.doc", "z23.doc", "z3.doc"];
a.sort(String.naturalCompare);
// ["z1.doc", "z2.doc", "z3.doc", "z10.doc", "z17.doc", "z23.doc"]

// Use wrapper function for case insensitivity
a.sort(function(a, b){
  return String.naturalCompare(a.toLowerCase(), b.toLowerCase());
})

// In most cases we want to sort an array of objects
var a = [ {"street":"350 5th Ave", "room":"A-1021"}
        , {"street":"350 5th Ave", "room":"A-21046-b"} ];

// sort by street, then by room
a.sort(function(a, b){
  return String.naturalCompare(a.street, b.street) || String.naturalCompare(a.room, b.room);
})

// When text transformation is needed (eg toLowerCase()),
// it is best for performance to keep
// transformed key in that object.
// There are no need to do text transformation
// on each comparision when sorting.
var a = [ {"make":"Audi", "model":"A6"}
        , {"make":"Kia",  "model":"Rio"} ];

// sort by make, then by model
a.map(function(car){
  car.sort_key = (car.make + " " + car.model).toLowerCase();
})
a.sort(function(a, b){
  return String.naturalCompare(a.sort_key, b.sort_key);
})
  • Works well with dates in ISO format eg "Rev 2012-07-26.doc".

Custom alphabet

It is possible to configure a custom alphabet to achieve a desired order.

// Estonian alphabet
String.alphabet = "ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy"
["t", "z", "x", "õ"].sort(String.naturalCompare)
// ["z", "t", "õ", "x"]

// Russian alphabet
String.alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя"
["Ё", "А", "Б"].sort(String.naturalCompare)
// ["А", "Б", "Ё"]

External links

Licence

Copyright (c) 2012-2015 Lauri Rooden <[email protected]>
The MIT License

org.webjars.npm

LiteJS

Versions

Version
1.4.0