Skip to content

Commit 0aa5094

Browse files
authored
merge: add alphanumerical sort (#872)
* alphanumerical sort * [/pull/872] add description to alphanumerical sort * [/pull/872] add description to localCompare fn * [/pull/872] code formatter
1 parent ef832f6 commit 0aa5094

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

Diff for: Sorts/AlphaNumericalSort.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
https://en.wikipedia.org/wiki/Natural_sort_order
3+
4+
In computing, natural sort order (or natural sorting) is the ordering of strings in alphabetical order,
5+
except that multi-digit numbers are treated atomically, i.e., as if they were a single character. Natural sort order
6+
has been promoted as being more human-friendly ("natural") than machine-oriented, pure alphabetical sort order.[1]
7+
8+
For example, in alphabetical sorting, "z11" would be sorted before "z2" because the "1" in the first string is sorted as smaller
9+
than "2", while in natural sorting "z2" is sorted before "z11" because "2" is treated as smaller than "11".
10+
11+
Alphabetical sorting:
12+
1.z11
13+
2.z2
14+
15+
Natural sorting:
16+
1. z2
17+
2. z11
18+
19+
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)
20+
21+
*/
22+
23+
const alphaNumericalSort = (a, b) => {
24+
/*
25+
https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
26+
27+
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.
28+
29+
The new locales and options arguments let applications specify the language whose sort order should be used and customize the behavior of the function.
30+
In older implementations, which ignore the locales and options arguments, the locale and sort order used are entirely implementation-dependent.
31+
Syntax:
32+
localeCompare(compareString, locales, options)
33+
34+
*/
35+
return a.localeCompare(b, undefined, { numeric: true })
36+
}
37+
38+
export { alphaNumericalSort }

Diff for: Sorts/test/AlphaNumericalSort.test.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { alphaNumericalSort } from '../AlphaNumericalSort'
2+
3+
describe('alphaNumericalComparer', () => {
4+
test('given array of eng symbols return correct sorted array', () => {
5+
const src = ['b', 'a', 'c']
6+
src.sort(alphaNumericalSort)
7+
expect(src).toEqual(['a', 'b', 'c'])
8+
})
9+
10+
test('given array of numbers return correct sorted array', () => {
11+
const src = ['15', '0', '5']
12+
src.sort(alphaNumericalSort)
13+
expect(src).toEqual(['0', '5', '15'])
14+
})
15+
16+
test('correct sort with numbers and strings', () => {
17+
const src = ['3', 'a1b15c', 'z', 'a1b14c']
18+
src.sort(alphaNumericalSort)
19+
expect(src).toEqual(['3', 'a1b14c', 'a1b15c', 'z'])
20+
})
21+
22+
test('correct sort with long numbers', () => {
23+
const src = ['abc999999999999999999999999999999999cba', 'abc999999999999999999999999999999990cba', 'ab']
24+
src.sort(alphaNumericalSort)
25+
expect(src).toEqual(['ab', 'abc999999999999999999999999999999990cba', 'abc999999999999999999999999999999999cba'])
26+
})
27+
28+
test('correct sort with z prefix', () => {
29+
const src = ['z', 'abc003def', 'abc1def', 'a']
30+
src.sort(alphaNumericalSort)
31+
expect(src).toEqual(['a', 'abc1def', 'abc003def', 'z'])
32+
})
33+
34+
test('correct sort with other language', () => {
35+
const src = ['а10б', 'а2б', 'в10г', 'в05г']
36+
src.sort(alphaNumericalSort)
37+
expect(src).toEqual(['а2б', 'а10б', 'в05г', 'в10г'])
38+
})
39+
})

0 commit comments

Comments
 (0)