Skip to content

Commit

Permalink
Fix ordering when a column only has numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
fcrespo82 committed Mar 5, 2021
1 parent 716c3ef commit 6d086d0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

## [2.1.4] - 2021-03-05

### Fixed
- Correct ordering when a column only has numbers

## [2.1.3] - 2020-08-14

### Removed
Expand Down
13 changes: 11 additions & 2 deletions src/sorter/MarkdownTableSortCodeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,16 @@ export class MarkdownTableSortCodeLensProvider implements vscode.CodeLensProvide
table.header[i] = cleanSortIndicator(header);
}
});

const canSortByNumber = table.body.every(l => parseFloat(l[headerIndex]))

switch (sortDirection) {
case MarkdownTableSortDirection.Asc:
table.body.sort((a: string[], b: string[]) => {
if (a[headerIndex].trim() === b[headerIndex].trim()) {
if (canSortByNumber) {
return (parseFloat(a[headerIndex]) < parseFloat(b[headerIndex])) ? -1 : 1;
}
else if (a[headerIndex].trim() === b[headerIndex].trim()) {
return 0;
}
else {
Expand All @@ -142,7 +148,10 @@ export class MarkdownTableSortCodeLensProvider implements vscode.CodeLensProvide
break;
case MarkdownTableSortDirection.Desc:
table.body.sort((a: string[], b: string[]) => {
if (a[headerIndex].trim() === b[headerIndex].trim()) {
if (canSortByNumber) {
return (parseFloat(a[headerIndex]) > parseFloat(b[headerIndex])) ? -1 : 1;
}
else if (a[headerIndex].trim() === b[headerIndex].trim()) {
return 0;
}
else {
Expand Down
6 changes: 6 additions & 0 deletions src/test/files/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
|column a 1|column b 1|
|column a 2|column b 2|

|header a|numeric header|
|------------|------------|
|column a 1|1|
|column a 2|10|
|column a 3|2|

## Lorem ipsum
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|Topic|Status|Notes|
Expand Down

0 comments on commit 6d086d0

Please sign in to comment.