Solved

Selecting & Sorting a Generic List of Custom Objects

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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