Lima
Lima

Reputation: 1211

Sorting Observable Collection incorrectly sorting

I have a WPF ObservableCollection which is bound to a ListBox and I have a Sort() method which when called will convert the ObservableCollection to a List(Of T), and undertakes a sort based on a date/time column within the collection.

The data is sorted, even when new items are added to the ObservableCollection, however the date/time isn't being correctly sorted. The data is sorting based on the date however it is very much random when it comes to the time portion. The following is an example of the outcomes I am experiencing:

Record-----Start Date
1          22/08/2011 22:00
3          22/08/2011 22:30
4          22/08/2011 14:00
2          22/08/2011 09:00
5          22/08/2011 21:00

In the above example, record 5 was the most recent addition, it was added after the initial binding.

My Sort() method is as follows:

Public Sub Sort()
  Dim SortedList As List(Of meetingDetails) = MyBase.ToList
  SortedList.Sort(New Comparison(Of meetingDetails)(Function(x As meetingDetails, _
                           y As meetingDetails)String.Compare(x.eStart, y.eStart)))
  For Each Item As meetingDetails In SortedList
    Move(IndexOf(Item), SortedList.IndexOf(Item))
    Next
End Sub

Is there anything that I am doing incorrectly in this method that would cause the time portion not be included in the sort? Is there a better way of doing a sort?

Thanks,

Matt

Upvotes: 0

Views: 393

Answers (1)

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84657

You seem to be using String.Compare instead of DateTime.Compare, try to change it and see if the sorting works

Upvotes: 1

Related Questions