Solved

Selecting & Sorting a Generic List of Custom Objects

Posted on 2014-04-02
5
247 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 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