StrataFrame Forum

Sorting dates in ListView

http://forum.strataframe.net/Topic20313.aspx

By Juan Carlos Pazos - 10/23/2008

Hi

I have a ListView that works fine, except in one thing.

If I click on any column header the data sorts fine, in numbers and text, but some columns has dates and here the data sort as "numbers", for example, if I have records these:

Date
24/09/2008
15/10/2008
02/10/2008
13/10/2008
10/10/2008
15/09/2008

After clic the column header I got this:

Date
02/10/2008
03/10/2008
10/10/2008
15/10/2008
15/09/2008
24/10/2008

My system date format is (Day/Month/Year), that's why the dates are in that format.

If I click again the order is 24, 15,15,10,03,02

In the Rowpopulating event I have this:

e.Values(6).DisplayValue = bo.FechaEnvio.ToShortDateString

The sorting is only counting the first digits (the day in my case), Is there a way to fix this, to sort correctly for the complete date?

Kindest regards

By Trent L. Taylor - 10/24/2008

The quickest way around this may be for you to create your own ListColumnSorter class.  You can copy of the SF column sorter and create your own.  It just uses an IComparable interface to sort the records.  This will give you control to sort the dates however you would like.  This is something that we can add to the list, but since it is character at this point, it will mean that we will have to sort differently for every region which will require a bit of energy (meaning time for implementation and testing at a global level for the framework).  You can move much faster on your own to get your needs met immediately.  But I will add this to the list to look at since I am pretty confident that a U.S. format is all that is being respected at the moment.
By Juan Carlos Pazos - 2/20/2009

Hi Trent

The time that I need to implement the date sorting, in your last message you taold me the the ListViewColumnSorter class implements the sort for the LV.

I finally arrives there, and I believe that the routine to modifiy is this:

''' <summary>

''' Compares two date values.

''' </summary>

''' <param name="x">The first value to compare.</param>

''' <param name="y">The second value to compare.</param>

''' <returns>An integer value indicating which value should be placed first.</returns>

''' <remarks></remarks>

Private Function CompareDates(ByVal x As DateTime, ByVal y As DateTime) As Integer

'-- Compare the two rows being tested

Select Case _SortOrder

Case SortOrder.Ascending

Return DateTime.Compare(x, y)

Case SortOrder.Descending

Return DateTime.Compare(y, x)

Case SortOrder.None

Return 0

End Select

End Function

But, I don't have any idea of what should I do with this, can you give me an idea of how or what should I do?

My date format is DD/MM/YYYY, in english is MM/DD/YYYY

I believe that I should format the date to the format I use, maybe something like this:

Return DateTime.Compare(CDate(Format(x, "d")), CDate(Format(y, "d")))

Hope you can point me in the right direction

Regards

By Trent L. Taylor - 2/23/2009

I looked at the ListViewColumnSorter and the dates should be getting sorted properly.  In the MicroFour StrataFrame Base assembly under the Windows folder, you will find the ListViewColumnSorter class.  It has the logic to determine if the column being sorted is a date.  If so, then it will use the CompareDates method and use the DateTime.Parse(...) method to convert the text into a datetime.

However, looking over the code, I see one thing that could be causing your date values to be sorted as a string instead of a date.  It has to do with the globalization delimitter.  However, to prove this, it would be helpful if you could step through this code or at the very least, give me a sample that reproduces your problem and we will just change the ListViewColumnSorter to make sure it is working in your language.  If you are having this issue then it is feasible that someone else may run into this also.

Also, be sure to give me the regional settings that you are using as well so that I can setup this as well.  Thanks.

By Juan Carlos Pazos - 2/23/2009

Hi Trent

I attach a sample proyect with database, you will see that some records for the second month (february) are miexed with the ones for january.

Also, be sure to give me the regional settings that you are using as well so that I can setup this as well.  Thanks.

Spanish/Mexico, for dates de Windows format is dd/MM/yyyy

Also I use this for spanish:

MicroFour.StrataFrame.UI.Localization.MessageLocaleID = 2058

Regards

By Trent L. Taylor - 2/24/2009

Juan,

Try this as it worked for me and did not require any change to the framework.  I set the column initial column sort index to the date column and it sorted as it should first thing:

So set the DefaultSortColumn = 2 and it should work correctly.

By Trent L. Taylor - 2/24/2009

OK...I found the problem.  It will require a change.  The column sorter evaluates the column type once and then attempts to sort using that type for all other columns.  So if the date is your default column, it will sort dates correctly and not the string and vice versa.  I will fix this now and it will be in the next beta posted.

Thanks for the sample!

By Juan Carlos Pazos - 2/24/2009

Trent

Thanks for your support and the time invested. I will wait for the next update.

Kindest regards

By Greg McGuffey - 2/24/2009

I might make the suggestion that you expose the ListViewSorter. That way, if there is some issue with the default provided (or some weird sorting needed), an alternative can easily be provided.
By Trent L. Taylor - 2/24/2009

Actually, it already is.  The ListView has a Sorter property that allows you to attach any sorter that you would like.  We just happen to attach the ListViewColumnSorter class by default.  But you can create your own as this is a function of the ListView control.
By Greg McGuffey - 2/24/2009

OK, I just took a look at the source and I need to be more specific. I know that I can use my own sorter with the listview and have done that (thanks to some previous help!). However, I was thinking that it'd be nice if I could provide my own sorter and get the nice auto sorting on column clicks etc. In order to get that currently, I need to provide not only the sorter, but also the code to handle column clicks, swap order on columns etc. (not a huge deal...but always looking to leverage the framework).



I see that this would actually a bit more work by you, as you not only have to expose the sorter, but also either use a base class or interface, so this would work. I still think this would be nice, but as you get your sorter worked out, there might be less of a need for what I'm asking.