From dca240dbba823620b559767e7f2dc5d7633757f1 Mon Sep 17 00:00:00 2001 From: RuSaG0 Date: Thu, 9 Dec 2021 18:30:37 +0300 Subject: [PATCH 1/4] alphanumerical sort --- Sorts/AlphaNumericalSort.js | 10 +++++++ Sorts/test/AlphaNumericalSort.test.js | 39 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 Sorts/AlphaNumericalSort.js create mode 100644 Sorts/test/AlphaNumericalSort.test.js diff --git a/Sorts/AlphaNumericalSort.js b/Sorts/AlphaNumericalSort.js new file mode 100644 index 0000000000..532f3380d3 --- /dev/null +++ b/Sorts/AlphaNumericalSort.js @@ -0,0 +1,10 @@ +/* + alpha numerical sort = Natural sort order + https://en.wikipedia.org/wiki/Natural_sort_order +*/ + +const alphaNumericalSort = (a, b) => { + return a.localeCompare(b, undefined,{ numeric: true }) +}; + +export { alphaNumericalSort } \ No newline at end of file diff --git a/Sorts/test/AlphaNumericalSort.test.js b/Sorts/test/AlphaNumericalSort.test.js new file mode 100644 index 0000000000..9f16d07186 --- /dev/null +++ b/Sorts/test/AlphaNumericalSort.test.js @@ -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']); + }); + // -------------------------- + 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г']); + }); +}); \ No newline at end of file From 2289c0aa717739d1fe6f18e33122c6449b96c356 Mon Sep 17 00:00:00 2001 From: RuSaG0 Date: Sun, 19 Dec 2021 20:50:17 +0300 Subject: [PATCH 2/4] [/pull/872] add description to alphanumerical sort --- Sorts/AlphaNumericalSort.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/Sorts/AlphaNumericalSort.js b/Sorts/AlphaNumericalSort.js index 532f3380d3..38c2a5e144 100644 --- a/Sorts/AlphaNumericalSort.js +++ b/Sorts/AlphaNumericalSort.js @@ -1,6 +1,24 @@ /* - alpha numerical sort = 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 + https://en.wikipedia.org/wiki/Natural_sort_order + https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare + + 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) => { From 5aa5d3f626c688f9c575ecb36138be62741e56b2 Mon Sep 17 00:00:00 2001 From: RuSaG0 Date: Mon, 20 Dec 2021 12:51:36 +0300 Subject: [PATCH 3/4] [/pull/872] add description to localCompare fn --- Sorts/AlphaNumericalSort.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Sorts/AlphaNumericalSort.js b/Sorts/AlphaNumericalSort.js index 38c2a5e144..4a205180b0 100644 --- a/Sorts/AlphaNumericalSort.js +++ b/Sorts/AlphaNumericalSort.js @@ -1,4 +1,6 @@ /* + 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] @@ -14,14 +16,23 @@ 1. z2 2. z11 - https://en.wikipedia.org/wiki/Natural_sort_order - https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare 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 }) }; From 8402828a938c72e14dce4f176d2181047c6460f2 Mon Sep 17 00:00:00 2001 From: RuSaG0 Date: Mon, 20 Dec 2021 18:09:16 +0300 Subject: [PATCH 4/4] [/pull/872] code formatter --- Sorts/AlphaNumericalSort.js | 25 ++++++----- Sorts/test/AlphaNumericalSort.test.js | 62 +++++++++++++-------------- 2 files changed, 43 insertions(+), 44 deletions(-) diff --git a/Sorts/AlphaNumericalSort.js b/Sorts/AlphaNumericalSort.js index 4a205180b0..fb6be8f424 100644 --- a/Sorts/AlphaNumericalSort.js +++ b/Sorts/AlphaNumericalSort.js @@ -1,22 +1,21 @@ /* 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 + 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 + + 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 + 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) */ @@ -24,16 +23,16 @@ 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. + + 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: + Syntax: localeCompare(compareString, locales, options) */ - return a.localeCompare(b, undefined,{ numeric: true }) -}; + return a.localeCompare(b, undefined, { numeric: true }) +} -export { alphaNumericalSort } \ No newline at end of file +export { alphaNumericalSort } diff --git a/Sorts/test/AlphaNumericalSort.test.js b/Sorts/test/AlphaNumericalSort.test.js index 9f16d07186..0e1c1c236e 100644 --- a/Sorts/test/AlphaNumericalSort.test.js +++ b/Sorts/test/AlphaNumericalSort.test.js @@ -1,39 +1,39 @@ -import {alphaNumericalSort} from "../AlphaNumericalSort"; +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']); - }); - // -------------------------- + const src = ['b', 'a', 'c'] + src.sort(alphaNumericalSort) + expect(src).toEqual(['a', 'b', 'c']) + }) + test('given array of numbers return correct sorted array', () => { - const src = ['15','0','5']; - src.sort(alphaNumericalSort); - expect(src).toEqual(['0','5','15']); - }); - // -------------------------- + 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']); - }); - // -------------------------- + 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']); - }); - // -------------------------- + 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']); - }); - // -------------------------- + 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г']); - }); -}); \ No newline at end of file + const src = ['а10б', 'а2б', 'в10г', 'в05г'] + src.sort(alphaNumericalSort) + expect(src).toEqual(['а2б', 'а10б', 'в05г', 'в10г']) + }) +})