Results 1 to 8 of 8

Thread: [RESOLVED] Sort array of servicecontroller

  1. #1

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

    Resolved [RESOLVED] Sort array of servicecontroller

    Hi, I have an array of ServiceController()

    For some reason some services are not sorted at first, so I need to sort it back order by servicename.

    I don't want to Do as loop
    So I do:
    Code:
     Private arr As ServiceController()
    .....
    Dim arrr As ServiceController()
    arrr = arr.OrderBy(Function(x) x.ServiceName).ToArray()
    arr = arrr
    Is there a more efficient way to do that?

    Thanks.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Sort array of servicecontroller

    The Enumerable.OrderBy method is just a wrapper for looping over the collection with a given predicate.

    You can do the same thing manually if you wanted to, but unless your collection contains several thousand items, the performance gain would be minimal if any. Not to mention there is the overhead of maintaining your own custom sort versus simply using a one-liner.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

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

    Re: Sort array of servicecontroller

    Yes it's only the PC services so it may be 200-300 the most.
    OK another question.
    I remember Shaggy provided a sorting option of the same array some years ago but I can't find the example and I can't remember .
    It was a one liner lambda also. So is it possible to sort this out without using another ServiceController as an intermediate?
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  4. #4

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

    Re: Sort array of servicecontroller

    Brain F#rt

    arr = arr.OrderBy(Function(x) x.ServiceName).ToArray()
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Sort array of servicecontroller

    Just don't declare the other array and convert the IEnumerable back to an array.
    Code:
    Private controllers() As ServiceController
    ' .....
    controllers = controllers.OrderBy(Function(controller) controller.ServiceName).ToArray()
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  6. #6

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

    Re: Sort array of servicecontroller

    Yes, again, brain f#rt
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  7. #7
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    557

    Re: [RESOLVED] Sort array of servicecontroller


  8. #8

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

    Re: [RESOLVED] Sort array of servicecontroller

    It not one dimensional so it will fail.
    Possible if you try with something like Array.Sort(of) but not sure.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

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