Results 1 to 7 of 7

Thread: Highly confused on how to order by objects inside an object

  1. #1

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,554

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    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.

  3. #3

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,554

    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.

    Name:  Clipboard01.jpg
Views: 142
Size:  32.2 KB

    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
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,624

    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

  5. #5

    Thread Starter
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,554

    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 ?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,991

    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:
    1. 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.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,372

    Re: Highly confused on how to order by objects inside an object

    For more information on sorting collections…

    http://www.scproject.biz/Sorting_Techniques.php

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width