Skip to content

Natural sort algorithm with unicode support written by Jim Palmer

Notifications You must be signed in to change notification settings

javve/natural-sort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

web component logo

Natural sort algorithm with unicode support

The algorithm is written by Jim Palmer and found at http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm/.

I, javve, only put it into a Component.

Installation

$ component install javve/natural-sort

Example

var naturalSort = require('natural-sort');

var values = ['B', 'a', 'D', 'c'];

values.sort(naturalSort); // ['B', 'D', 'a', 'c']

values.sort(function(a, b) {
    return naturalSort(a, b, { insensitive: true });
}); // ['a', 'B', 'c', 'D']

values.sort(function(a, b) {
    return naturalSort(a, b, { insensitive: true, desc: true })
}); // ['D', 'c', 'B', 'a']


var values = [
    { val: 'B' },
    { val: 'a' },
    { val: 'D' },
    { val: 'c' }
];

values.sort(function(a, b) {
    return naturalSort(a.val, b.val, { insensitive: true });
}); // ['a', 'B', 'c', 'D']

Find more examples at http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm/ or look at the tests in /test. It's quite impressive. Handles dates, etc.

License

MIT

About

Natural sort algorithm with unicode support written by Jim Palmer

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published