-
Feb 9th, 2021, 01:09 PM
#1
[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.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 9th, 2021, 01:20 PM
#2
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.
-
Feb 9th, 2021, 01:29 PM
#3
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?
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 9th, 2021, 01:34 PM
#4
Re: Sort array of servicecontroller
Brain F#rt
arr = arr.OrderBy(Function(x) x.ServiceName).ToArray()
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 9th, 2021, 01:37 PM
#5
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()
-
Feb 9th, 2021, 01:39 PM
#6
Re: Sort array of servicecontroller
Yes, again, brain f#rt
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 9th, 2021, 01:42 PM
#7
Re: [RESOLVED] Sort array of servicecontroller
-
Feb 9th, 2021, 02:07 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|