1

I'm wondering how one would sort a column of decimal or currency values numerically in show-listview?

A contrived example:

$decimalArray = 1.1, 2.2, 3.3, 10.0, 20.0, 30.0

$props = @{
        Title = "Decimal Sorting"
        InfoTitle = "Decimal Sorting"
        InfoDescription = "Decimal Sorting"
        PageSize = 100000
    }
$decimalArray | Show-ListView @props -Property @{Label = "Decimals"; Expression={$_}},
    @{Label = "Currency"; Expression={"{0:c}" -f $_}},
    @{Label = "Number"; Expression={"{0:n0}" -f $_}}
Close-Window

In the example above:

  1. The first column is a decimal
  2. The second column is currency
  3. The third column is an integer

Only the integer sorts numerically. It seems as soon as a decimal or currency symbol is added to the value, ListView treats the data as a string.

Might I sort decimals and currency numerically?

Thank you, vic

| improve this question | | | | |
1

Spe uses the class ListViewComparer to handle the sorting. It appears as of 6.1 it sorts DateTime, int, and finally string.

You can submit a feature request or a pull request if you would like to see more datatypes covered.

public int Compare(String str1, String str2)
{
    DateTime dateTime1, dateTime2;
    if (DateTime.TryParse(str1, out dateTime1) && DateTime.TryParse(str2, out dateTime2))
    {
        return DateTime.Compare(dateTime1, dateTime2);
    }

    int int1, int2;
    if (int.TryParse(str1, out int1) && int.TryParse(str2, out int2))
    {
        if (int1 < int2)
            return -1;
        return int1 > int2 ? 1 : 0;
    }

    return String.Compare(str1, str2, StringComparison.Ordinal);
}
| improve this answer | | | | |
  • Thank you very much Michael. – user7110063 May 5 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.