Need to sort ListView columns that contain dates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • **Developer**

    Need to sort ListView columns that contain dates

    I need to sort the columns of a ListView.

    Some columns contain dates and others contain integers.

    What I did once before is in the Compare method I tried date and if that
    failed I did Integer.

    Seems kinda not nice - is there a better way?

    Thanks
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
    Implements IComparer.Compa re

    -snip

    ''Cast the objects to be compared to ListViewItem objects.

    lListviewX = CType(x, ListViewItem)

    lListviewY = CType(y, ListViewItem)

    Try

    'Parse the two objects passed as a parameter as a DateTime.

    Dim lFirstDate As System.DateTime =
    DateTime.Parse( lListviewX.SubI tems(mColumnToS ort).Text)

    Dim lSecondDate As System.DateTime =
    DateTime.Parse( lListviewY.SubI tems(mColumnToS ort).Text)

    'Compare the two dates.

    lCompareResult = DateTime.Compar e(lFirstDate, lSecondDate)

    ' Catch

    'If neither compared object has a valid date format compare the two items
    as a string

    lCompareResult =
    mInsensitiveCom pare.Compare(lL istviewX.SubIte ms(mColumnToSor t).Text,
    lListviewY.SubI tems(mColumnToS ort).Text) 'Or can do this

    End Try



  • Carlos J. Quintero [.NET MVP]

    #2
    Re: Need to sort ListView columns that contain dates

    Can´t you know in advance which is the data type of each column? If not then
    you have to guess it, but I would do that before comparing, that is, take
    the first listitem, guess if the column contains date or integer and then
    call a different comparer for each case.

    --
    Best regards,

    Carlos J. Quintero

    MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
    You can code, design and document much faster.
    Free resources for add-in developers:
    MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


    " **Developer**" <REMOVEdevelope r@a-znet.com> escribió en el mensaje
    news:emKtsqqeFH A.228@TK2MSFTNG P12.phx.gbl...[color=blue]
    >I need to sort the columns of a ListView.
    >
    > Some columns contain dates and others contain integers.
    >
    > What I did once before is in the Compare method I tried date and if that
    > failed I did Integer.
    >
    > Seems kinda not nice - is there a better way?
    >
    > Thanks
    > Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
    > Implements IComparer.Compa re
    >
    > -snip
    >
    > ''Cast the objects to be compared to ListViewItem objects.
    >
    > lListviewX = CType(x, ListViewItem)
    >
    > lListviewY = CType(y, ListViewItem)
    >
    > Try
    >
    > 'Parse the two objects passed as a parameter as a DateTime.
    >
    > Dim lFirstDate As System.DateTime =
    > DateTime.Parse( lListviewX.SubI tems(mColumnToS ort).Text)
    >
    > Dim lSecondDate As System.DateTime =
    > DateTime.Parse( lListviewY.SubI tems(mColumnToS ort).Text)
    >
    > 'Compare the two dates.
    >
    > lCompareResult = DateTime.Compar e(lFirstDate, lSecondDate)
    >
    > ' Catch
    >
    > 'If neither compared object has a valid date format compare the two items
    > as a string
    >
    > lCompareResult =
    > mInsensitiveCom pare.Compare(lL istviewX.SubIte ms(mColumnToSor t).Text,
    > lListviewY.SubI tems(mColumnToS ort).Text) 'Or can do this
    >
    > End Try
    >
    >
    >[/color]


    Comment

    • **Developer**

      #3
      Re: Need to sort ListView columns that contain dates

      This is a generally used usercontrol so that would be difficult unless I
      required the form containing the control to do something to help.

      Can't I get the type in the method somehow?

      Thanks

      "Carlos J. Quintero [.NET MVP]" <carlosq@NOSPAM sogecable.com> wrote in
      message news:O$yJbPveFH A.2180@TK2MSFTN GP12.phx.gbl...[color=blue]
      > Can´t you know in advance which is the data type of each column? If not
      > then you have to guess it, but I would do that before comparing, that is,
      > take the first listitem, guess if the column contains date or integer and
      > then call a different comparer for each case.
      >
      > --
      > Best regards,
      >
      > Carlos J. Quintero
      >
      > MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
      > You can code, design and document much faster.
      > Free resources for add-in developers:
      > http://www.mztools.com
      >
      > " **Developer**" <REMOVEdevelope r@a-znet.com> escribió en el mensaje
      > news:emKtsqqeFH A.228@TK2MSFTNG P12.phx.gbl...[color=green]
      >>I need to sort the columns of a ListView.
      >>
      >> Some columns contain dates and others contain integers.
      >>
      >> What I did once before is in the Compare method I tried date and if that
      >> failed I did Integer.
      >>
      >> Seems kinda not nice - is there a better way?
      >>
      >> Thanks
      >> Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer
      >> Implements IComparer.Compa re
      >>
      >> -snip
      >>
      >> ''Cast the objects to be compared to ListViewItem objects.
      >>
      >> lListviewX = CType(x, ListViewItem)
      >>
      >> lListviewY = CType(y, ListViewItem)
      >>
      >> Try
      >>
      >> 'Parse the two objects passed as a parameter as a DateTime.
      >>
      >> Dim lFirstDate As System.DateTime =
      >> DateTime.Parse( lListviewX.SubI tems(mColumnToS ort).Text)
      >>
      >> Dim lSecondDate As System.DateTime =
      >> DateTime.Parse( lListviewY.SubI tems(mColumnToS ort).Text)
      >>
      >> 'Compare the two dates.
      >>
      >> lCompareResult = DateTime.Compar e(lFirstDate, lSecondDate)
      >>
      >> ' Catch
      >>
      >> 'If neither compared object has a valid date format compare the two items
      >> as a string
      >>
      >> lCompareResult =
      >> mInsensitiveCom pare.Compare(lL istviewX.SubIte ms(mColumnToSor t).Text,
      >> lListviewY.SubI tems(mColumnToS ort).Text) 'Or can do this
      >>
      >> End Try
      >>
      >>
      >>[/color]
      >
      >[/color]


      Comment

      • Carlos J. Quintero [.NET MVP]

        #4
        Re: Need to sort ListView columns that contain dates

        No, the type is text always, so you must guess its format.

        --
        Best regards,

        Carlos J. Quintero

        MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
        You can code, design and document much faster.
        Free resources for add-in developers:
        MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


        " **Developer**" <REMOVEdevelope r@a-znet.com> escribió en el mensaje
        news:e4Ga%23Oxe FHA.228@TK2MSFT NGP12.phx.gbl.. .[color=blue]
        > This is a generally used usercontrol so that would be difficult unless I
        > required the form containing the control to do something to help.
        >
        > Can't I get the type in the method somehow?
        >
        > Thanks
        >[/color]

        Comment

        • **Developer**

          #5
          Re: Need to sort ListView columns that contain dates

          Now that I think of it that makes sense

          Thanks


          "Carlos J. Quintero [.NET MVP]" <carlosq@NOSPAM sogecable.com> wrote in
          message news:eb8%23xSxe FHA.3280@TK2MSF TNGP09.phx.gbl. ..[color=blue]
          > No, the type is text always, so you must guess its format.
          >
          > --
          > Best regards,
          >
          > Carlos J. Quintero
          >
          > MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
          > You can code, design and document much faster.
          > Free resources for add-in developers:
          > http://www.mztools.com
          >
          > " **Developer**" <REMOVEdevelope r@a-znet.com> escribió en el mensaje
          > news:e4Ga%23Oxe FHA.228@TK2MSFT NGP12.phx.gbl.. .[color=green]
          >> This is a generally used usercontrol so that would be difficult unless I
          >> required the form containing the control to do something to help.
          >>
          >> Can't I get the type in the method somehow?
          >>
          >> Thanks
          >>[/color]
          >[/color]


          Comment

          • Doug Taylor

            #6
            Re: Need to sort ListView columns that contain dates

            On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
            <carlosq@NOSPAM sogecable.com> wrote:
            [color=blue]
            >No, the type is text always, so you must guess its format.[/color]

            You could always inherit from the list view and extend the columns
            property by having the column type as an attribute and then call the
            appropriate comparator.

            I had a similar issue with a list view that shows information from a
            database table, here I used the column index returned on the column
            header click event and used that to refer into the datatable and find
            out what the database definition of that column's type was.

            Doug Taylor

            Comment

            • Carlos J. Quintero [.NET MVP]

              #7
              Re: Need to sort ListView columns that contain dates

              Hi Doug,

              I know what you mean, but that would require some collaboration from the
              user of the usercontrol, wouldn´t it? I mean, if the user passes or uses the
              classes of the listview instead of the extended ones from the inherited
              listview...


              --
              Best regards,

              Carlos J. Quintero

              MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
              You can code, design and document much faster.
              Free resources for add-in developers:
              MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


              "Doug Taylor" <Doug.TaylorNTP @tayNOSPAMmade. demon.co.uk> escribió en el
              mensaje news:0620c1lbh3 2pjq6b7nn5o0gg4 b7omatun6@4ax.c om...[color=blue]
              > On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
              > <carlosq@NOSPAM sogecable.com> wrote:
              >
              > You could always inherit from the list view and extend the columns
              > property by having the column type as an attribute and then call the
              > appropriate comparator.
              >
              > I had a similar issue with a list view that shows information from a
              > database table, here I used the column index returned on the column
              > header click event and used that to refer into the datatable and find
              > out what the database definition of that column's type was.
              >
              > Doug Taylor
              >[/color]


              Comment

              • **Developer**

                #8
                Re: Need to sort ListView columns that contain dates


                "Doug Taylor" <Doug.TaylorNTP @tayNOSPAMmade. demon.co.uk> wrote in message
                news:0620c1lbh3 2pjq6b7nn5o0gg4 b7omatun6@4ax.c om...[color=blue]
                > On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
                > <carlosq@NOSPAM sogecable.com> wrote:
                >[color=green]
                >>No, the type is text always, so you must guess its format.[/color]
                >
                > You could always inherit from the list view and extend the columns
                > property by having the column type as an attribute and then call the
                > appropriate comparator.
                >[/color]

                I think that will work niceky. I already have a ColumnAdd method and will
                simply insert an optional type parameter.

                Thanks alot



                [color=blue]
                > I had a similar issue with a list view that shows information from a
                > database table, here I used the column index returned on the column
                > header click event and used that to refer into the datatable and find
                > out what the database definition of that column's type was.
                >
                > Doug Taylor
                >[/color]


                Comment

                • **Developer**

                  #9
                  Re: Need to sort ListView columns that contain dates

                  I could develop a enum for the different types but I wonder if there is
                  already something I could use?

                  That is:

                  Public Sub ColumnAdd(ByVal header As String......,op tional byval columnType
                  as ???=string)


                  " **Developer**" <REMOVEdevelope r@a-znet.com> wrote in message
                  news:OBac1cyeFH A.3280@TK2MSFTN GP09.phx.gbl...[color=blue]
                  >
                  > "Doug Taylor" <Doug.TaylorNTP @tayNOSPAMmade. demon.co.uk> wrote in message
                  > news:0620c1lbh3 2pjq6b7nn5o0gg4 b7omatun6@4ax.c om...[color=green]
                  >> On Mon, 27 Jun 2005 14:35:07 +0200, "Carlos J. Quintero [.NET MVP]"
                  >> <carlosq@NOSPAM sogecable.com> wrote:
                  >>[color=darkred]
                  >>>No, the type is text always, so you must guess its format.[/color]
                  >>
                  >> You could always inherit from the list view and extend the columns
                  >> property by having the column type as an attribute and then call the
                  >> appropriate comparator.
                  >>[/color]
                  >
                  > I think that will work niceky. I already have a ColumnAdd method and will
                  > simply insert an optional type parameter.
                  >
                  > Thanks alot
                  >
                  >
                  >
                  >[color=green]
                  >> I had a similar issue with a list view that shows information from a
                  >> database table, here I used the column index returned on the column
                  >> header click event and used that to refer into the datatable and find
                  >> out what the database definition of that column's type was.
                  >>
                  >> Doug Taylor
                  >>[/color]
                  >
                  >[/color]


                  Comment

                  • **Developer**

                    #10
                    Re: Need to sort ListView columns that contain dates


                    "Carlos J. Quintero [.NET MVP]" <carlosq@NOSPAM sogecable.com> wrote in
                    message news:OOYrJYyeFH A.3808@TK2MSFTN GP14.phx.gbl...[color=blue]
                    > Hi Doug,
                    >
                    > I know what you mean, but that would require some collaboration from the
                    > user of the usercontrol, wouldn´t it? I mean, if the user passes or uses
                    > the classes of the listview instead of the extended ones from the
                    > inherited listview...[/color]


                    Good point, I'd have to check to be sure I have a type stored for the column
                    and if not do what I'm doing now.

                    I'm assuming one of the problems whith using Try-Catch is that it is slow.
                    Is that right??


                    With or without Doug's suggestion,
                    Would it be better if when the routine is entered it checks to see if a type
                    is stored for that column and if not figure it out and store it.

                    Then

                    Select case storedTypeForCo lumn(..)
                    case int

                    case string
                    ..
                    ..
                    ..

                    That way it would only check once per column.



                    Comment

                    • Carlos J. Quintero [.NET MVP]

                      #11
                      Re: Need to sort ListView columns that contain dates


                      " **Developer**" <REMOVEdevelope r@a-znet.com> escribió en el mensaje
                      news:%23Ph%23Tu yeFHA.3712@TK2M SFTNGP09.phx.gb l...[color=blue]
                      > I'm assuming one of the problems whith using Try-Catch is that it is
                      > slow. Is that right??[/color]

                      Yes, exceptions should be avoided, but if you do it only once it is fine.

                      [color=blue]
                      > With or without Doug's suggestion,
                      > Would it be better if when the routine is entered it checks to see if a
                      > type is stored for that column and if not figure it out and store it.[/color]

                      That's error prone. For example, you get "1": is it a string or an integer
                      number? You may guess that it is a number. Later you get "1.1" for that same
                      column -> the type is likely to be float number...or you get "A" -> the type
                      was not an integer number, it was a string after all...


                      --
                      Best regards,

                      Carlos J. Quintero

                      MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
                      You can code, design and document much faster.
                      Free resources for add-in developers:
                      MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.




                      Comment

                      • Carlos J. Quintero [.NET MVP]

                        #12
                        Re: Need to sort ListView columns that contain dates

                        You can use System.TypeCode

                        --
                        Best regards,

                        Carlos J. Quintero

                        MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
                        You can code, design and document much faster.
                        Free resources for add-in developers:
                        MZ-Tools has a single goal: To make your everyday programming life easier. As an add-in to several Integrated Development Environment (IDEs) from Microsoft, MZ-Tools adds new menus and toolbars to them that provide many new productivity features.


                        " **Developer**" <REMOVEdevelope r@a-znet.com> escribió en el mensaje
                        news:%231Myejye FHA.3280@TK2MSF TNGP09.phx.gbl. ..[color=blue]
                        >I could develop a enum for the different types but I wonder if there is
                        >already something I could use?
                        >
                        > That is:
                        >
                        > Public Sub ColumnAdd(ByVal header As String......,op tional byval
                        > columnType as ???=string)
                        >[/color]

                        Comment

                        • **Developer**

                          #13
                          Re: Need to sort ListView columns that contain dates


                          "Carlos J. Quintero [.NET MVP]" <carlosq@NOSPAM sogecable.com> wrote in
                          message news:ePA13MzeFH A.688@TK2MSFTNG P14.phx.gbl...[color=blue]
                          > You can use System.TypeCode[/color]

                          Thanks



                          Comment

                          • **Developer**

                            #14
                            Re: Need to sort ListView columns that contain dates


                            "Carlos J. Quintero [.NET MVP]" <carlosq@NOSPAM sogecable.com> wrote in
                            message news:OWxpGLzeFH A.1448@TK2MSFTN GP14.phx.gbl...[color=blue]
                            >
                            > " **Developer**" <REMOVEdevelope r@a-znet.com> escribió en el mensaje
                            > news:%23Ph%23Tu yeFHA.3712@TK2M SFTNGP09.phx.gb l...[color=green]
                            >> I'm assuming one of the problems whith using Try-Catch is that it is
                            >> slow. Is that right??[/color]
                            >
                            > Yes, exceptions should be avoided, but if you do it only once it is fine.
                            >
                            >[color=green]
                            >> With or without Doug's suggestion,
                            >> Would it be better if when the routine is entered it checks to see if a
                            >> type is stored for that column and if not figure it out and store it.[/color]
                            >
                            > That's error prone. For example, you get "1": is it a string or an integer
                            > number? You may guess that it is a number. Later you get "1.1" for that
                            > same column -> the type is likely to be float number...or you get "A" ->
                            > the type was not an integer number, it was a string after all...
                            >
                            >
                            > --[/color]
                            Actually I did think about that.

                            As long as I'm giving the user a way of telling, I'll probably just assume
                            string if not told.

                            Thanks again


                            Comment

                            Working...