-
Sep 7th, 2023, 03:40 AM
#1
Highly confused on how to order by objects inside an object
This confuses me.
I have this code:
Code:
Private Sub SurroundingSub()
Dim schcls As Schedule = New Schedule()
schcls.Screens = schm.Screens
Dim foundDocuments As List(Of ScheduleScreen) = schcls.Screens.OrderBy(Function(o) o.ScreenNumber).ToList()
For Each sr As ScheduleScreen In schcls.Screens
sr.Sessions.OrderBy((Function(o) o.SessionStartString).ToList()
Next
End Sub
Of course this will not work as it's immutable but you get the idea.
The result must be and order by on SessionStartString ( that is Ulong date representation such as 20230907170000
Is there an automatic way? Should I create a new list and start adding? Then again how would I add sorted results with Linq? Or do I need some comparisons?
Thanks.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Sep 7th, 2023, 03:48 AM
#2
Re: Highly confused on how to order by objects inside an object
OrderBy doesn't sort a list. It creates a new list that contains the same items as the original list but in the order specified. If you're not assigning the result to something, you're not doing anything useful. Just as you're doing an assignment outside the loop, you would have to do an assignment inside the loop too.
That said, if the Sessions property is a List(Of T) then why call OrderBy at all? That List already has its own Sort method that allows you to sort it in place, so there's no assignment necessary.
-
Sep 7th, 2023, 04:13 AM
#3
Re: Highly confused on how to order by objects inside an object
Thanks.
Not sure how would I sort by a property of the sessions.

What I currently did was to do a for each to select the ScheduleScreen that I'm interested and then on a new clear ScheduleScreenSession do an orderby
so:
Code:
Dim scrscd as new List (Of ScheduleScreen)
'' add just the one anticipated screen
ses = scrscd(0).Sessions.OrderBy((Function(o) o.SessionStartString).ToList()
That seems to work.
Last edited by sapator; Sep 7th, 2023 at 04:34 AM.
Reason: corrected language
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Sep 7th, 2023, 01:27 PM
#4
Re: Highly confused on how to order by objects inside an object
You can largely sort by whatever you want. The approach you take depends on what ELSE you want to sort by. Those look like custom classes, so if you implement IComparable, then the Sort will use whatever comparator you provide in the implementation of the interface. Of course, that largely means that you just have one way to sort the object, which is whatever way you provided. If you won't ALWAYS want to sort in that fashion, there are overloads of Sort that takes either an IComparison(of T) or a Comparer(of T), either of which allows you to pass in a custom comparison method to use in that particular sort.
My usual boring signature: Nothing
 
-
Sep 7th, 2023, 04:31 PM
#5
Re: Highly confused on how to order by objects inside an object
I remember another IComparable , i think it was you that suggested it but I remember it been very limited at the time and had to be exact, or maybe it was another I--. Thing is there is no sort on the session only orderby. Anyhow I'm fine with what I did but out of curiosity how would I sort by Screens.Session.SessionStartString ?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Sep 8th, 2023, 01:09 AM
#6
Re: Highly confused on how to order by objects inside an object
Please provide a FULL and CLEAR explanation of the problem. It appears that there are multiple sessions so how do you propose to sort by a property of those objects? Are you just talking about sorting the sessions within a screen? Spell it out clearly, perhaps providing an example of what have and what you expect.
Like I said, if you have an object that has a property that is a list, you can't sort that list in place using OrderBy. OrderBy creates a new list, so you'd have to assign that to the property. If you want to sort a List(Of T) in place then you can call its own Sort method and you can call Array.Sort to do the same for arrays. In those cases you can use a Comparison(Of T) delegate to compare items, e.g.
vb.net Code:
myList.Sort(Function(item1, item2) item1.SomeProperty.CompareTo(item2.SomeProperty))
You might like to follow the Blog link in my signature and check out my post on sorting arrays and collections.
-
Sep 8th, 2023, 07:13 AM
#7
Re: Highly confused on how to order by objects inside an object
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|