Sorting dates in ListView


Author
Message
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
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.
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.
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.
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...

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!

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.

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 (131 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
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

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
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.
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