ministreak
Member
- Joined
- Apr 14, 2006
- Messages
- 11
- Programming Experience
- 5-10
Hi,
I’m using VS 2005
I have two arrays, both integers. I need to sort them as a pare in reverse order. I cannot get this to work. It will sort them in order but will not reverse them. i.e. the highest Key starting at index 0.
Then I thought it was because I was using integers. So I filled a couple of arrays with strings (as shown below) and got the same results.
This code was taken straight from Microsoft’s MSDN and is supposed to reverse the results in the both arrays. Is anyone familiar with this than can help?
I got this code at the following address:
http://msdn2.microsoft.com/en-us/lib...18(VS.71).aspx
I know that there is something simple that I'm not seeing.
Also the [ code ] [ /code ] tags don't seem to color nor format the vb code to well.. sorry. I put the spaces in here on purpose so it would leave them as text in this post.
Thanks for any help you can offer.
.
I’m using VS 2005
I have two arrays, both integers. I need to sort them as a pare in reverse order. I cannot get this to work. It will sort them in order but will not reverse them. i.e. the highest Key starting at index 0.
Then I thought it was because I was using integers. So I filled a couple of arrays with strings (as shown below) and got the same results.
This code was taken straight from Microsoft’s MSDN and is supposed to reverse the results in the both arrays. Is anyone familiar with this than can help?
I got this code at the following address:
http://msdn2.microsoft.com/en-us/lib...18(VS.71).aspx
VB.NET:
[SIZE=2][COLOR=#0000ff]Imports[/COLOR][/SIZE][SIZE=2] System.Collections[/SIZE]
[SIZE=2][COLOR=#0000ff]Public [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] MyReverseClass [/SIZE][SIZE=2][COLOR=#008000]'Class used to compare and sort arrays[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Implements[/COLOR][/SIZE][SIZE=2] IComparer[/SIZE]
[SIZE=2][COLOR=#008000]'Calls CaseInsensitiveComparer.Compare with parameters reversed.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] Compare([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [Object], [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] y [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [Object]) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] IntegerImplements IComparer.Compare[/SIZE]
[SIZE=2][COLOR=#0000ff]Return [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] CaseInsensitiveComparer().Compare(y, x)[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'IComparer.Compare[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] aKeys() AsString = {[/SIZE][SIZE=2][COLOR=#a31515]"Yellow"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]"Green"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]"Purple"[/COLOR][/SIZE][SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] aValues() AsString = {[/SIZE][SIZE=2][COLOR=#a31515]"Banana"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]"Pear"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]"Grape"[/COLOR][/SIZE][SIZE=2]}[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] MyComparer [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MyReverseClass() [/SIZE][SIZE=2][COLOR=#008000]'Used to compare and sort arrays using the class MyReverseClass[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Regular sort which works.............[/COLOR][/SIZE]
[SIZE=2]Array.Sort(aKeys, aValues)[/SIZE]
[SIZE=2]MsgBox(aKeys(0) & [/SIZE][SIZE=2][COLOR=#a31515]"="[/COLOR][/SIZE][SIZE=2] & aValues(0) & vbCrLf & aKeys(1) & [/SIZE][SIZE=2][COLOR=#a31515]"="[/COLOR][/SIZE][SIZE=2] & aValues(1) & vbCrLf & aKeys(2) & [/SIZE][SIZE=2][COLOR=#a31515]"="[/COLOR][/SIZE][SIZE=2] & aValues(2))[/SIZE]
[SIZE=2][SIZE=2][COLOR=#008000]'Reverse sort which works BUT does NOT reverse the data.............[/COLOR][/SIZE]
[/SIZE][SIZE=2]Array.Sort(aKeys, aValues, MyComparer)[/SIZE]
[SIZE=2]MsgBox(aKeys(0) & [/SIZE][SIZE=2][COLOR=#a31515]"="[/COLOR][/SIZE][SIZE=2] & aValues(0) & vbCrLf & aKeys(1) & [/SIZE][SIZE=2][COLOR=#a31515]"="[/COLOR][/SIZE][SIZE=2] & aValues(1) & vbCrLf & aKeys(2) & [/SIZE][SIZE=2][COLOR=#a31515]"="[/COLOR][/SIZE][SIZE=2] & aValues(2))[/SIZE]
[SIZE=2][COLOR=#008000]'Both sort methods produce the same results below[/COLOR][/SIZE]
[SIZE=2]Green=Pear[/SIZE]
[SIZE=2]Purple=Grape[/SIZE]
[SIZE=2]Yellow=Banana[/SIZE]
[SIZE=2][SIZE=2][COLOR=#008000]'I would like the results to come out reversed as shown here:[/COLOR][/SIZE]
[/SIZE][SIZE=2]Yellow=Banana[/SIZE]
[SIZE=2]Purple=Grape[/SIZE]
[SIZE=2]Green=Pear[/SIZE]
I know that there is something simple that I'm not seeing.
Also the [ code ] [ /code ] tags don't seem to color nor format the vb code to well.. sorry. I put the spaces in here on purpose so it would leave them as text in this post.
Thanks for any help you can offer.
.