-5

I am adding data in a collection which I want to sort based on the date. I want to organize the data in the collection in such a way that all the past dates or current dates should come at top, and all the future dates should come at the bottom, after past current dates.

I am doing something like this as mentioned below

objList.Name = ' Here I am getting the name
objList.CurDate = ' Here I am getting the current date
obJList.EventDate = ' Here I am getting the event date

then I am adding the above record in a collection.

Dim colEvents As New Collection
colEvents.Add(objList)

After this I want to check if EventDate <= CurDate, then sort the data in the collection such that past and current dates are arranged at the top, and future dates come after past and current dates.

NOTE: EventDate can be past, future, or current date.

Can someone please help me understand how can I achieve this?

djv
  • 15,168
  • 7
  • 48
  • 72
Omi
  • 427
  • 7
  • 21
  • 42
  • Why do you have CurDate in each object? Isn't it the same for each one (today)? – djv Dec 13 '16 at 18:42
  • 1
    And are you stuck with `Collection`? There are much better ways to store data with .NET – djv Dec 13 '16 at 18:46
  • And what is the point of checking if the event date is in the past before sorting, i.e. *check- if EventDate <= CurDate then sort*? – djv Dec 13 '16 at 18:50
  • 3
    It will be too long to give you full answer. Here is the tip. Dump `Collection`, use generic `List(Of T)`. Populate data into `YourObjectModel`. Use `System.Linq` for sorting and manipulating collections. In your case `List(Of YourObjectModel)` – T.S. Dec 13 '16 at 19:00
  • @Verdolino Hi, I want to manipulate using two date time value i.e. one CurDate which is the current date of the system and EvenDate which I will be getting from the user. My local development machine date and the systems current date is not same. – Omi Dec 14 '16 at 01:50
  • @T.S. Hi, actually I have already developed the logic using collection since I want to loop through each record i.e. the reason why I want to continue with collection. – Omi Dec 14 '16 at 01:51
  • 1
    Independently of what you say, there is absolutely **no reason** to use `Collection`. This type is obsolete and only exists to support old code. For new code, use generic collection. I am sure that everything you've written to loop your collection, can be replaced by List without changing much. – T.S. Dec 14 '16 at 02:03
  • @T.S. So you mean to say I can't do it what I am trying to do using Collection?? Instead of collection its better to use List??? – Omi Dec 14 '16 at 03:42
  • @T.S. Yes I am working on an old piece of code. – Omi Dec 14 '16 at 03:43
  • If you are modifying old code - this is new code unless you're still using framework 2.0. Old code - means compile without modification. Then again, LINQ gives enormous flexibility and capability written for you, so you don't have to. Collections is not strongly typed and generics are. For the what Tiny Giant does - he is trying to improve your post to attract someone to answer it. This is part of SO strategy – T.S. Dec 14 '16 at 04:12
  • @T.S. Thanks a lot lemme just try first. Ooh.. I am sorry I was not sure of SO strategy. – Omi Dec 14 '16 at 04:15
  • Possible duplicate of [VB.net, How to sort collection items by value](http://stackoverflow.com/questions/16923301/vb-net-how-to-sort-collection-items-by-value) – djv Dec 14 '16 at 16:11

1 Answers1

0

There is no native way to sort a Microsoft.VisualBasic.Collection. I suggest converting to a newer type such as List(Of T).

Anyway, here is an example of converting a Collection to a List(Of T)

Define the class which holds your events. You didn't provide it. I'll guess

Class myEvent
    Public Property Name As String
    Public Property CurDate As DateTime
    Public Property EventDate As DateTime
End Class

Now make a few events and add them to the collection. I'm adding them out of order

Dim colEvents As New Collection()

Dim objList1 As New myEvent()
objList1.Name = "Name1"
objList1.EventDate = New DateTime(2016, 12, 20)
objList1.CurDate = DateTime.Now
colEvents.Add(objList1)

Dim objList2 As New myEvent()
objList2.Name = "Name2"
objList2.EventDate = New DateTime(2016, 12, 5)
objList2.CurDate = DateTime.Now
colEvents.Add(objList2)

Dim objList3 As New myEvent()
objList3.Name = "Name3"
objList3.EventDate = New DateTime(2016, 12, 10)
objList3.CurDate = DateTime.Now
colEvents.Add(objList3)

Here is where it can be converted to a List(Of myEvent), and sorted at the same time. Oh, since it's not a primitive type, you must supply a function to OrderBy so it knows how to sort. This function simply returns the EventDate

Dim eventsList As New List(Of myEvent)(
    colEvents.
    OfType(Of myEvent).
    OrderBy(Function(l As myEvent) l.EventDate))

Just to confirm the order, print the contents of both objects

Console.WriteLine("Collection:")
For Each c As myEvent In colEvents
    Console.WriteLine(
        String.Format("Name: {0}, Event date: {1}",
                      c.Name, c.EventDate))
Next

Console.WriteLine("List:")
For Each l In eventsList
    Console.WriteLine(
        String.Format("Name: {0}, Event date: {1}",
                      l.Name, l.EventDate))
Next

Output:

Collection:
Name: Name1, Event date: 12/20/2016 12:00:00 AM
Name: Name2, Event date: 12/5/2016 12:00:00 AM
Name: Name3, Event date: 12/10/2016 12:00:00 AM
List:
Name: Name2, Event date: 12/5/2016 12:00:00 AM
Name: Name1, Event date: 12/10/2016 12:00:00 AM
Name: Name3, Event date: 12/20/2016 12:00:00 AM

I interpreted your statement I want to check if EventDate <= CurDate, then sort the data in the collection such that past and current dates are arranged at the top, and future dates come after past and current dates, to simply mean that you want to sort in ascending order, and that CurDate doesn't serve any purpose. If this is wrong then please clarify and we can fix the OrderBy lambda in my example.

And if you are really stuck with Collection, see this similar question related to sorting VBA collections

Community
  • 1
  • 1
djv
  • 15,168
  • 7
  • 48
  • 72