1.4.0 • Published 9 years ago

natural-compare-lite v1.4.0

Weekly downloads
92,661
License
MIT
Repository
github
Last release
9 years ago
@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 <lauri@rooden.ee>
The MIT License

@typescript-eslint/eslint-plugineasy-select-rnjobot-ciquoc-testluminos-ui-coresklif-ui-kitsklif-apits-handy@everything-registry/sub-chunk-2249jawwy-sdkjawwy_gamification_releasereact-native-sphereuisphereuijawwy_libraryreact-native-credit-card-pkgp149-tablesklif-uireact-native-jawwy_samplegulp-natural-sortjrennsoh88-react-native-scroll-indicatorjordy-frijters-test-libhauhdahdoahdoagriffin-ui-libraryjulien-easy-modalmavectrajawwy_library_newjawy_library_v1gamification-jawwy-libraryframework_test_library_sixdee_new_jawwytest_lib_module_aarzzzxxxyyy321123eslint-plugin-perfectionisteslint-plugin-perfectionist-unofficialeslint-plugin-ninjaeslint-plugin-typescript-custom-sort-keyseslint-plugin-typescript-sorteslint-plugin-typescript-sort-keyseslint-plugin-sort-destructure-keysframework_test_library_sixdeeframework_test_library_sixdee_newframework_test_library_sixdee_new_newfmsleditorjs_forkededitor.js-custom-multiple-selectedexpo-renavigatefawaterak-online-paymentfawatrak-online-paymentgalen-utilsfigma-sort-layersfluent.adflow.reactnativesdkfluent.adflow.reactnativesdk-alphagamification-integration-newgaurav-react-native-loopgenz-native-elementsp147-tablep148-tablenative-modal-damage-vehiclenew-awesome-4321mtva-teleport-toolmy-library-buttonpnm-yph-react-native-custom-componentsmicroend-componentpickupbiz-npm-packagequickcapture_react_nativereact-ang-notifications-honey-comb-setupreact-flow-highly-customizable-edgesqaapio-reecord-mp3react-native-custom-image-carouselreact-native-ctp-odpreact-native-dhp-printerreact-native-dimensions-layoutreact-native-innity-2react-native-innity-remasterreact-native-input-libraryreact-native-ghn-ekycreact-native-get-countriesreact-native-fedlight-dsmreact-lightbox-pack-18supportreact-microphone-recorder1react-native-auth-service-clientreact-native-basic-appreact-native-basic-screenreact-native-additionreact-native-animate-textreact-native-android-video-player-viewreact-native-awesome-module-latestreact-native-awesome-android-123react-native-awesome-android-123-zeotapreact-native-bleccs-componentsreact-native-bluetooth-device-detectreact-native-build-vesion-getterreact-native-bridge-packagereact-native-chenaarreact-native-check-componentreact-native-conekta-card-tokenizerreact-native-components-designrn-agora-ios-mreact-tailwindcss-datepicker-costume-2reat-native-multiplierkprreactofy-css-library
1.4.0

9 years ago

1.3.1

9 years ago

1.3.0

9 years ago

1.2.2

10 years ago

1.2.0

10 years ago

1.1.0

10 years ago

1.0.0

10 years ago

0.4.7

10 years ago

0.4.6

10 years ago

0.4.5

10 years ago

0.4.4

10 years ago

0.4.3

11 years ago

0.4.2

11 years ago

0.4.1

11 years ago

0.4.0

11 years ago

0.3.3

11 years ago

0.3.2

11 years ago

0.3.1

11 years ago

0.3.0

11 years ago

0.2.0

11 years ago

0.1.0

11 years ago

0.0.1

11 years ago