Sorting dates in ListView


Author
Message
Juan Carlos Pazos
Juan Carlos Pazos
StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)
Group: Forum Members
Posts: 144, Visits: 227
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

Smile Everything is possible, just keep trying...

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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.
Juan Carlos Pazos
Juan Carlos Pazos
StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)
Group: Forum Members
Posts: 144, Visits: 227
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

Smile Everything is possible, just keep trying...

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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.

Juan Carlos Pazos
Juan Carlos Pazos
StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)
Group: Forum Members
Posts: 144, Visits: 227
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

Smile Everything is possible, just keep trying...

Attachments
DatesTest.zip (130 views, 253.00 KB)
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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.

Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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!

Juan Carlos Pazos
Juan Carlos Pazos
StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)StrataFrame User (228 reputation)
Group: Forum Members
Posts: 144, Visits: 227
Trent

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

Kindest regards

Smile Everything is possible, just keep trying...

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
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.
Trent Taylor
Trent Taylor
StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)StrataFrame Developer (10K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 6.9K
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.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search