Skip to content

Commit

Permalink
fix(stringSorter): Use localeCompare in the stringSorter to prevent i…
Browse files Browse the repository at this point in the history
…ncorrect sorting when mixed case

fix HAWNG-7
  • Loading branch information
mmelko authored and tadayosi committed Nov 24, 2023
1 parent a23b6c5 commit 3bce079
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
20 changes: 19 additions & 1 deletion packages/hawtio/src/util/strings.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { humanizeLabels, matchWithWildcard, parseBoolean, toString, trimQuotes } from './strings'
import { humanizeLabels, matchWithWildcard, parseBoolean, stringSorter, toString, trimQuotes } from './strings'

describe('strings', () => {
test('toString', () => {
Expand Down Expand Up @@ -59,4 +59,22 @@ describe('strings', () => {
expect(matchWithWildcard(value, '*apache.c*')).toBe(true)
expect(matchWithWildcard(value, '*ap*e.c*')).toBe(true)
})

describe('stringSorter', () => {
expect(stringSorter('a-string', 'b-string')).toBe(-1)
expect(stringSorter('a-string', 'B-string')).toBe(-1)
expect(stringSorter('A-string', 'B-string')).toBe(-1)
expect(stringSorter('A-string', 'b-string')).toBe(-1)

expect(stringSorter('a-string', 'b-string', true)).toBe(1)
expect(stringSorter('a-string', 'B-string', true)).toBe(1)
expect(stringSorter('A-string', 'B-string', true)).toBe(1)
expect(stringSorter('A-string', 'b-string', true)).toBe(1)

expect(stringSorter('string', 'String')).toBe(-1)
expect(stringSorter('string', 'string')).toBe(0)

expect(stringSorter('string-7', 'string-8')).toBe(-1)
expect(stringSorter('string-7', 'string-8', true)).toBe(1)
})
})
8 changes: 1 addition & 7 deletions packages/hawtio/src/util/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,7 @@ export function trimQuotes(text: string): string {
}

export function stringSorter(a: string, b: string, sortDesc?: boolean): number {
let res = 0
if (a < b) {
res = -1
}
if (a > b) {
res = 1
}
let res = a.localeCompare(b)
if (sortDesc) {
res *= -1
}
Expand Down

0 comments on commit 3bce079

Please sign in to comment.