I am trying to sort on a date column, but it New-UDTable appears to be sorting it as a string. I have confirmed the field being returned from SQL is a data time.
Hi Zak, do you need the time part of your date? I’m sure I had this same issue in UD, so I just converted the datetime in SQL to just a short date, then everything worked in the sorting without me having to do anything fancy.
I’m sure if you pass it as a PSCUSTOM object the data and use something like [datetime] to force the data to be in that format…I been using UD for a while, and I am sure I did this for one of my grids passing data as a string instead of an int…sorry I don’t have an example to hand at the moment, but hopefully you get what I am saying? By all means raise this as an issue on github and I am sure this will get fixed in a future release
@ZHumphries
i have found a fix so far that worked and show the format as you want “MM/dd/yyyy” or “dd/MM/yyyy”
let me know if you still need assistant and ill post how to apply the fix.
Seems like others are not having the issue with sorting PowerShell date times not sorting. Would someone mind trying the following dashboard and see if it produces the same issue as I am seeing?
I guess im thinking too powershell(y) here, i.e. pass it a [datetime] object and it just deals with it as you would expect. The fact it accept a Get-Date object just reinforces that.
Ill have a play with passing it a string, but I guess I should probably look into server side sorting as doing it this way doesnt really provide a good end user experience (weirdly formatted time)
Thanks for the attempt, unfortunately also its possible to get the in ital sort working (by removing the comment) clicking the column to sort still produces the same result.
Create a class containing data for one line. It should look like this:
class MyObject {
# A valid time string
[string] $TimeString
# Adds a get property to the class, allows a computed value as property instead of a function
# https://stackoverflow.com/a/40365941
hidden $_generateProperty= $(
# Allows sorting of date / time
$this | Add-Member ScriptProperty 'SortTime' `
{
# If time is unknown, set it to this string, will group all unknown if sorting
# If time is known, take the filedatetime, which is comparably to: YYYYMMDDHHmm
if([string]::IsNullOrEmpty($this.TimeString) -ne $true){
$Time = (Get-Date -Date $this.TimeString-Format FileDateTime).ToString()
} else {
$Time = "unknown"
}
# Return computed value
$Time
}
)
}
So, you define a get property, called “SortTime”, which you have to reference then as Property in the table.