473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Question about arrays

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
3 1129
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

"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
19
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
5
by: Wajih-ur-Rehman | last post by:
The question is about C++ (since its the C family, i posted it on this newsgroup) Lets say i declare an array int a = {1,2,3,4}; int *p = a; //This is allowed because "a" returns the address of...
3
by: James dean | last post by:
I have created algorithms in C# unsafe code and have fixed the arrays in memory for optimum performance. I use multidimensional arrays rather than jagged arrays. The algorithms i use usually read a...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
2
by: phpCodeHead | last post by:
Hello fellow codemeisters! I am needing to parse through two lists of serial numbers for parts being received into an inventory database. First, is the list of serial numbers already in the...
31
by: mdh | last post by:
I am still having a problem understanding K&RII on p 112. I have looked at the FAQs --which I am sure answer it in a way that I have missed, so here goes. A 2-dim array, (per K&R) is really a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.