Solved

Selecting & Sorting a Generic List of Custom Objects

Posted on 2014-04-02
5
248 Views
Last Modified: 2016-02-16
Hi,

I have a Generic Collection of a custom type that I have created. Short of using a LINQ query  (which I have never used before), I need to sort the collection based on an individual element (a time stamp) so I can retrieve the newest data added to the collection for a given UserID.

    Structure AuthenticationRecord
        Dim UserID As String
        Dim UserPW As String
        Dim AuthLevel As String
        Dim TS As Date
    End Structure

    Dim AuthenticationDataCache As List(Of AuthenticationRecord)

    Private Sub Test()

        Dim AR As AuthenticationRecord

        AR.UserID = "TestUserName1"
        AR.UserPW = "TestPW1"
        AR.AuthLevel = "3"
        AR.TS = Now.AddMilliseconds(-1000)
        AuthenticationDataCache.Add(AR)

        AR.UserID = "TestUserName1"
        AR.UserPW = "TestPW1"
        AR.AuthLevel = "-1"
        AR.TS = Now()
        AuthenticationDataCache.Add(AR)

        'Here is where I get stuck....
        'My need is to retrieve the newest Record in the collection (as defined by TS). In the above
        'example, the record with AuthLevel = -1 is most recent (based on TS)
        'I either need a LINQ Query to do this, or to sort the contents of the collection
        'with the most recent TS record item towards the top, then I would iterate through the collection
        'from the beginning to find the first occuring UserID I was looking for.


    End Sub

Open in new window

0
Comment
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Assisted Solution

by:jayakrishnabh
jayakrishnabh earned 50 total points
ID: 39974294
Dim sortedCollection = From p In AuthenticationDataCacheOrder By p.TS Descendingp

'Collection now would be sorted.
'After this you can iterate either through For or For Each loop to get the first element
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39975301
Hi RobertFromSecretWeapons;

Using Linq will be the better way to go here. Using @ jayakrishnabh solution correcting a couple of typos.

Dim sortedCollection = From p In AuthenticationDataCache
                       Order By p.TS Descending

Open in new window

0
 

Author Comment

by:RobertFromSecretWeapons
ID: 39976543
Well, heck, that was simple and nice.

Assuming that the collection would have many instances of same AND different UserID's, could I extend that LINQ query to sort first and then retrieve whatever UserID I was looking for (being the most recent record added to the collection as specified by TS.  That's ultimately what I need.

That would be super nice.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 300 total points
ID: 39976641
Hi RobertFromSecretWeapons;

The following query will return one Latest record for the user "TestUserName1"

Dim lookingFor As String = "TestUserName1"

Dim sortedCollection = From p In AuthenticationDataCache
                       Group p By Key = p.UserID Into Group
                       Where Key = lookingFor
                       Select Group.OrderByDescending(Function(t) t.UserID).FirstOrDefault()

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 150 total points
ID: 39976758
You don't *have* to use LINQ for this. The List class defines a Sort method, one overload of which takes in a Comparison delegate--which means it also accepets an anonymous function.

e.g.

AuthenticationDataCache.Sort(Function (left, right) left.TS.CompareTo(right.TS))

Open in new window


You would then have the list in ascending order, so just grab the last element. If you'd rather do descending order, so you can grab the first element (rather than the last), you can simply switch "right" and "left" in the anonymous function body:

AuthenticationDataCache.Sort(Function (left, right) right.TS.CompareTo(left.TS))

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question