-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
add alphanumerical sort #872
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| https://en.wikipedia.org/wiki/Natural_sort_order | ||
|
|
||
| In computing, natural sort order (or natural sorting) is the ordering of strings in alphabetical order, | ||
| except that multi-digit numbers are treated atomically, i.e., as if they were a single character. Natural sort order | ||
| has been promoted as being more human-friendly ("natural") than machine-oriented, pure alphabetical sort order.[1] | ||
|
|
||
| For example, in alphabetical sorting, "z11" would be sorted before "z2" because the "1" in the first string is sorted as smaller | ||
| than "2", while in natural sorting "z2" is sorted before "z11" because "2" is treated as smaller than "11". | ||
|
|
||
| Alphabetical sorting: | ||
| 1.z11 | ||
| 2.z2 | ||
|
|
||
| Natural sorting: | ||
| 1. z2 | ||
| 2. z11 | ||
|
|
||
|
|
||
| P.S. use this function, as there are a lot of implementations on the stackoverflow and other forums, but many of them don't work correctly (can't pass all my tests) | ||
|
|
||
| */ | ||
|
|
||
| const alphaNumericalSort = (a, b) => { | ||
| /* | ||
| https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare | ||
|
|
||
| The localeCompare() method returns a number indicating whether a reference string comes before, or after, or is the same as the given string in sort order. | ||
|
|
||
| The new locales and options arguments let applications specify the language whose sort order should be used and customize the behavior of the function. | ||
| In older implementations, which ignore the locales and options arguments, the locale and sort order used are entirely implementation-dependent. | ||
| Syntax: | ||
| localeCompare(compareString, locales, options) | ||
|
|
||
| */ | ||
| return a.localeCompare(b, undefined,{ numeric: true }) | ||
| }; | ||
|
|
||
| export { alphaNumericalSort } | ||
|
raklaptudirm marked this conversation as resolved.
Outdated
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import {alphaNumericalSort} from "../AlphaNumericalSort"; | ||
|
|
||
| describe('alphaNumericalComparer', () => { | ||
| test('given array of eng symbols return correct sorted array', () => { | ||
| const src = ['b','a','c']; | ||
| src.sort(alphaNumericalSort); | ||
| expect(src).toEqual(['a','b','c']); | ||
| }); | ||
| // -------------------------- | ||
|
raklaptudirm marked this conversation as resolved.
Outdated
|
||
| test('given array of numbers return correct sorted array', () => { | ||
| const src = ['15','0','5']; | ||
| src.sort(alphaNumericalSort); | ||
| expect(src).toEqual(['0','5','15']); | ||
| }); | ||
| // -------------------------- | ||
| test('correct sort with numbers and strings', () => { | ||
| const src = ['3','a1b15c','z','a1b14c']; | ||
| src.sort(alphaNumericalSort); | ||
| expect(src).toEqual(['3','a1b14c','a1b15c','z']); | ||
| }); | ||
| // -------------------------- | ||
| test('correct sort with long numbers', () => { | ||
| const src = ['abc999999999999999999999999999999999cba','abc999999999999999999999999999999990cba','ab']; | ||
| src.sort(alphaNumericalSort); | ||
| expect(src).toEqual(['ab','abc999999999999999999999999999999990cba','abc999999999999999999999999999999999cba']); | ||
| }); | ||
| // -------------------------- | ||
| test('correct sort with z prefix', () => { | ||
| const src = ['z','abc003def','abc1def','a']; | ||
| src.sort(alphaNumericalSort); | ||
| expect(src).toEqual(['a','abc1def','abc003def','z']); | ||
| }); | ||
| // -------------------------- | ||
| test('correct sort with other language', () => { | ||
| const src = ['а10б','а2б','в10г','в05г']; | ||
| src.sort(alphaNumericalSort); | ||
| expect(src).toEqual(['а2б', 'а10б', 'в05г', 'в10г']); | ||
| }); | ||
| }); | ||
|
raklaptudirm marked this conversation as resolved.
Outdated
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.