Question about arrays

Q

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
 
L

Lloyd Sheen

QDL said:
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
 
G

Guest

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
 
A

Armin Zingler

QDL said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top