By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

Question about arrays

P: n/a
QDL
Hello everyone,

I have a very simple question about arrays I have an array of Processes
objects (retrieved using Process.GetProcesses()). I want to sort them
descending on the WorkingSet size.

Is this code correct?

Dim temp as Process
Dim mProcessi() as Process
Dim i, j, n as Integer
n = mProcessi.GetUpperBound(0)
For i = 0 To n - 1
For j = i + 1 To n
If mProcessi(i).WorkingSet < mProcessi(j).WorkingSet Then
temp = mProcessi(i)
mProcessi(i) = mProcessi(j)
mProcessi(j) = temp
End If
Next
Next

I think it is all right. Temp is just a pointer to an instance of Process as
all elements in mProcessi array are, so doing so I just sort the pointers to
the objects and I should not mess with memory, resources and so on...

TIA
Paolo

Sep 13 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Paolo,

The ".Net way" to sort the array would be to create an class that implements
IComparer and use the IComparer class as an argument to the array's Sort
method.

For example:

Public Class ProcessWorkingSetSorter
Implements IComparer(Of Process)

Public Function Compare(ByVal x As System.Diagnostics.Process, ByVal y
As System.Diagnostics.Process) As Integer Implements
System.Collections.Generic.IComparer(Of System.Diagnostics.Process).Compare

Return x.WorkingSet64.CompareTo(y.WorkingSet64)

End Function
End Class

Now, to sort the array:

Array.Sort(mProcessi, New ProcessWorkingSetSorter)

Kerry Moorman
"QDL" wrote:
Hello everyone,

I have a very simple question about arrays I have an array of Processes
objects (retrieved using Process.GetProcesses()). I want to sort them
descending on the WorkingSet size.

Is this code correct?

Dim temp as Process
Dim mProcessi() as Process
Dim i, j, n as Integer
n = mProcessi.GetUpperBound(0)
For i = 0 To n - 1
For j = i + 1 To n
If mProcessi(i).WorkingSet < mProcessi(j).WorkingSet Then
temp = mProcessi(i)
mProcessi(i) = mProcessi(j)
mProcessi(j) = temp
End If
Next
Next

I think it is all right. Temp is just a pointer to an instance of Process as
all elements in mProcessi array are, so doing so I just sort the pointers to
the objects and I should not mess with memory, resources and so on...

TIA
Paolo

Sep 13 '07 #2

P: n/a

"QDL" <du***@dummy.comwrote in message
news:46**********************@reader4.news.tin.it. ..
Hello everyone,

I have a very simple question about arrays I have an array of Processes
objects (retrieved using Process.GetProcesses()). I want to sort them
descending on the WorkingSet size.

Is this code correct?

Dim temp as Process
Dim mProcessi() as Process
Dim i, j, n as Integer
n = mProcessi.GetUpperBound(0)
For i = 0 To n - 1
For j = i + 1 To n
If mProcessi(i).WorkingSet < mProcessi(j).WorkingSet Then
temp = mProcessi(i)
mProcessi(i) = mProcessi(j)
mProcessi(j) = temp
End If
Next
Next

I think it is all right. Temp is just a pointer to an instance of Process
as all elements in mProcessi array are, so doing so I just sort the
pointers to the objects and I should not mess with memory, resources and
so on...

TIA
Paolo
Have not checked if your code would work, but the dot.net way is to use one
of the Sort functions of the Array. I would use the Sort method which takes
an object which implements the IComparer interface. Create a class that
implements this, create an instance of the class and then your sort code is
encapsulated into a class and can be resued.

Hope this helps
Lloyd Sheen

Sep 13 '07 #3

P: n/a
"QDL" <du***@dummy.comschrieb
Hello everyone,

I have a very simple question about arrays I have an array of Processes
objects (retrieved using Process.GetProcesses()). I want to sort them
descending on the WorkingSet size.

Is this code correct?

Does ist work? ;-) Looks ok.

Dim temp as Process
Dim mProcessi() as Process
Dim i, j, n as Integer
n = mProcessi.GetUpperBound(0)
For i = 0 To n - 1
For j = i + 1 To n
If mProcessi(i).WorkingSet < mProcessi(j).WorkingSet Then
temp = mProcessi(i)
mProcessi(i) = mProcessi(j)
mProcessi(j) = temp
End If
Next
Next

I think it is all right. Temp is just a pointer to an instance of Process
as all elements in mProcessi array are, so doing so I just sort the
pointers to the objects and I should not mess with memory, resources and
so on...

Another (nice) way: (VB 2005 only)

Array.Sort(Of Process)(mProcessi, New Comparer)

'...

Class Comparer
Implements IComparer(Of Process)

Public Function Compare( _
ByVal x As System.Diagnostics.Process, _
ByVal y As System.Diagnostics.Process) As Integer _
Implements System.Collections.Generic.IComparer(Of
System.Diagnostics.Process).Compare

Return x.WorkingSet64.CompareTo(y.WorkingSet64)
End Function
End Class
I used WorkingSet64 because I get a "deprecated" warning for WorkingSet.
(The class looks only that long due to line continuation for this post...)
Armin

Sep 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.