EvotecIT / PSWriteHTML

PSWriteHTML is PowerShell Module to generate beautiful HTML reports, pages, emails without any knowledge of HTML, CSS or JavaScript. To get started basics PowerShell knowledge is required.
MIT License
826 stars 106 forks source link

Unable to sort multiple columns in opposite directions #241

Closed jasonrush closed 3 years ago

jasonrush commented 3 years ago

I am trying to create a custom ticketing dashboard, and am running into an issue. I would like to sort first by priority of tickets (high = 1/med = 2/low = 3) and then by ticket ID (larger means newer) so I see high priority tickets first, then newest to oldest. Problem is that means Priority should be sorting Descending (1,2,3) but ID should be sorting Ascending (3,2,1). I've verified I can specify multiple sort columns (int or string arrays depending on specifying column indexes or names) via arrays, but I can only specify one way to sort the columns. Is there any solution/recommendation on how I might be able to do this as-is, or is this a limitation to the current implementation?

https://github.com/EvotecIT/PSWriteHTML/blob/e62d662b1df9bef11c95cd3e8656bae0ef5b051e/Public/New-HTMLTable.ps1#L31-L33

Here's an example of what I'm after (sorting by PRI descending, then ID ascending):

PRI ID Summary
1 7 Placeholder Text
1 5 Placeholder Text
2 4 Placeholder Text
2 2 Placeholder Text
2 1 Placeholder Text
3 6 Placeholder Text
3 3 Placeholder Text
jasonrush commented 3 years ago

Looking at this a bit more, I think the solution would be to convert $DefaultSortOrder from [string] to [string[]], and within the foreach() loops create some kind of localized $sort variable. And at this point I've put in enough effort I should just create a PR...

https://github.com/EvotecIT/PSWriteHTML/blob/e62d662b1df9bef11c95cd3e8656bae0ef5b051e/Public/New-HTMLTable.ps1#L33

https://github.com/EvotecIT/PSWriteHTML/blob/e62d662b1df9bef11c95cd3e8656bae0ef5b051e/Public/New-HTMLTable.ps1#L706-L726

Additional note: From reviewing this chunk of code, I'd imagine lines 714 and 722 (if ($Column -ne - 1) {) should be -1 and not - 1 with a space?

PrzemyslawKlys commented 3 years ago

I am not sure if we should do it this way:

I'm trying to understand the difference between the current solution and the solution from the first link.

Yes, this is a bug (-1). Most likely introduce while formatting 🙀

PrzemyslawKlys commented 3 years ago

Two bugs:

PrzemyslawKlys commented 3 years ago

Ok, going thru the implementation. By default when you use DefaultSortColumn it will always sort it according to DefaultSortOrder. If there are 5 columns to sort on, It will have to be either all ascending or all descending.

There are 2 choices:

  1. Implement two arrays as you say but then deal with problems with having disprepency 2 to 1, 5 to 1, 7 to 3 between columns. It's possible possible people will use different
  2. Implement it in the New-TableColumnOption and allow to define it per column

Maybe even have both done.

https://github.com/EvotecIT/PSWriteHTML/blob/96e672bacf109d87868867f489b9e44fcae8d744/Public/New-TableColumnOption.ps1#L1-L1

jasonrush commented 3 years ago

I've thrown together a PR (https://github.com/EvotecIT/PSWriteHTML/pull/242) that I think should fix it in a non-breaking way (backwards compatible, but makes reasonable enough sense). Haven't actually run/tested it yet.

PrzemyslawKlys commented 3 years ago

I'll take a look tomorrow - my brain is shutting down. Thank you for your effort.

jasonrush commented 3 years ago

No, thank you for the awesome module! I've only dabbled with it a couple times, but it's fantastic to work with both on the coding side and the outputs.