array.sort(aKeys, sValues, MyComparer)

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

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.
.
 
VB.NET:
Dim strings() As String = {"abc", "def", "ghi"}
Array.Sort(strings)
Array.Reverse(strings)
 
Solution to array sort in reverse.

This example uses a Console application for the output, but it shows you how it works.


Sub Main()
Dim aKeys() As String = {"Yellow", "Green", "Purple"}
Dim aValues() As String = {"Banana", "Pear", "Grape"}
Array.Sort(aKeys, aValues)
For x As Integer = 0 To 2
Console.Write(aKeys(x))
Console.WriteLine(aValues(x))
Next x
Console.WriteLine()
Array.Reverse(aKeys)
Array.Reverse(aValues)
For x As Integer = 0 To 2
Console.Write(aKeys(x))
Console.WriteLine(aValues(x))
Next x
End Sub
 
Well it works for strings but what about integers?

Thanks guys.

Really good thinking there. I had never used the Array.Reverse(array) before. That worked great with strings.

But when I go back to my original problem which uses integers in both arrays the problem is not sloved. See Below:

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] aKeys() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] aValues() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = {14, 32, 65, 4, 50, 19, 19, 19, 23, 1}[/SIZE]
[SIZE=2]Array.Sort(aKeys)[/SIZE]
[SIZE=2]Array.Sort(aValues)[/SIZE]
[SIZE=2]Array.Reverse(aKeys)[/SIZE]
[SIZE=2]Array.Reverse(aValues)[/SIZE]
[SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]This is how the original values would look before any processing:[/COLOR][/SIZE]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]'1 = 14[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'2 = 32[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'3 = 65[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'4 = 4[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'5 = 50[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'6 = 19[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'7 = 19[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'8 = 19[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'9 = 23[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'10 = 1[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#008000]'I want to sory by Values but in reverse order and the keys must[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'also change into the same position as the values. The results should[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'come out as follows:[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'10 = 1[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'4 = 4[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'1 = 14[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'6 = 19[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'7 = 19 NOTE: Keys 7,8,9 don't really matter as the values are all 19,19,19[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'8 = 19[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'9 = 23[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'2 = 32[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'5 = 50[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'3 = 65[/COLOR][/SIZE]
 
[COLOR=#008000]'The keys are still equal to the same values except that the values are now[/COLOR]
[COLOR=#008000]ordered in REVERSE order.  The order of the keys makes no difference here.[/COLOR]


That is why I was trying to use the code I found on MSDN that I listed in my original post.

Your expertise is appreciated.
 
I kinda suspected that, but was more hoping you didn't know the difference between an Integer and a String, and that your variable naming was pure coincidence. lol :)

What you are trying to say is that those two arrays are linked like a Dictionary; keys and values, and that they both are integer arrays, right? There is no way you can sort both independently and maintain the index relationship. You have to create a class (T) with both members key/value, add instances of this to a list collection (Of T), and implement the IComparer(Of T) to be used as sorter (or use a Comparison(Of T) delegate). This is very simple:
VB.NET:
Class uggabugga
 
    Public key, value As Integer
 
    Sub New(ByVal key As Integer, ByVal value As Integer)
        Me.key = key
        Me.value = value
    End Sub
End Class
VB.NET:
Class ubReverseSorter
    Implements IComparer(Of uggabugga)
 
    Public Function Compare(ByVal x As uggabugga, ByVal y As uggabugga) As Integer _
    Implements System.Collections.Generic.IComparer(Of uggabugga).Compare
        Return x.value > y.value
    End Function
End Class
VB.NET:
Sub MakeSomeNoise()
   [COLOR=darkgreen]'some sample data[/COLOR]
    Dim lars As New List(Of uggabugga)
    lars.Add(New uggabugga(1, 14))
    lars.Add(New uggabugga(2, 32))
    lars.Add(New uggabugga(3, 65))
    lars.Add(New uggabugga(4, 4))
    [COLOR=darkgreen]'sort it[/COLOR]
    lars.Sort(New ubReverseSorter)
    [COLOR=darkgreen]'let's have a look[/COLOR]
    Dim sb As New System.Text.StringBuilder
    For Each ub As uggabugga In lars
        sb.AppendFormat("key:{0} value:{1}", ub.key, ub.value)
        sb.AppendLine()
    Next
    MsgBox(sb.ToString)
End Sub
 
Interesting John,
I haven’t tried any of this as of yet but I think you for the time you put into this example display. You mean there is a difference in integers and strings? I really would have never known had you not said something… whew! To think I’ve been programming VB since 99 and didn’t realize that…

Seriously now,
I find it strange that you would have a different method of doing this than the one outlined above in my first post that Microsoft recommends. The example shown by Microsoft using the link in my original post above looks to be pretty easy. And in their example it worked but they were using strings but didn’t say you couldn’t use integers…

The only reason I am trying to use 2 arrays loaded with integers is because there is no way that I have found to sort a two dimension array using only one of the elements. I would love to be able to use MyArray(100,100) and sort it in REVERSE ORDER using EITHER the first or second element. I could load it either way and it would work and the code would be much cleaner.

My question are to you are:
1.) Would the code you provided work on a 2 dimension array?
2.) Why do you choose this method of using the two arrays over Microsofts recommendations?
3.) Or, is there an easier way to use a 2 dimension array of integers and sort in REVERSE order using only one of the elements?

Thanks for your point of view on this topic John
 
Here is your solution.

You need to include both parallel arrays in the Array.Sort argument list. The first argument is the array that will be sorted. The second argument is the parallel array that will correspond to the first array.

The Array.Reverse method will only work with an array that has already been sorted. It has only one argument, so you need to do it twice, once with each of the arrays. Since they have already been sorted with corresponding values, this is not a problem.

It makes absolutely no difference whether the array is a string or an integer. Your mistake was in not placing the parallel array arguments in the proper order. Note that the Array.Sort method uses 2 arguments and places the Values array first, because that is the primary key that you want to sort. The following code will do exactly what you want. Copy the code to a Console app or revise the Console WriteLine statements to display the results in a Windows Forms app.


Sub Main()
Dim bKeys() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Dim bValues() As Integer = {14, 32, 65, 4, 50, 19, 19, 19, 23, 1}

Array.Sort(bValues, bKeys)

For x As Integer = 0 To 9
Console.Write(bKeys(x) & " ")
Console.WriteLine(bValues(x))
Next x

Console.WriteLine()

Array.Reverse(bKeys)
Array.Reverse(bValues)

For x As Integer = 0 To 9
Console.Write(bKeys(x) & " ")
Console.WriteLine(bValues(x))
Next x
End Sub
 
Last edited:
I have to say that this was an awesome piece of code, which should be put into a sticky!

Instead if using the MyComparer to reverse the order as in
Array.Sort(aValues, aKeys, MyComparer).
I just needed to use the Array.Sort(aValues, aKeys) then use REVERSE on them……….

No need for the third parameter “MyComparer” nor the extra class…


How simple!!

Beautiful piece of work. Took only 30 seconds to implement and the results were right on!

Thank you... Thank you... Thank you…
And thank you John for keeping this thread interesting…
 
Last edited:
ministreak:
1. No. it is not operating on array, it is operating on a strongly typed collection, generics (Of T) is new with .Net 2.0, Object and late binding is a ghost of the past, so is in many cases arrays. The System.Array.Sort method is also only for one-dimensional array.
2. What recommendation is that?
3. Solitaire found it. (for 2 one-dimensional paired arrays, dictionary like)

Your question about the fictional myarray(100,100) I'm not sure about, you say first or second element?? It has 10201 elements and each 101 elements is anonymously related somehow (rows or columns). This translates to a collection containing 101 elements of a class with 101 fields/properties. Not sure about your questions and purpose with this code. You can also implement your own QuickSort for multidimensions, but it will be an awful lot of swapping, http://en.wikipedia.org/wiki/Quicksort

Solitaire:
Nice! I very rarely use arrays, so I wasn't familiar with this sort overload.
 
Hi John,
Sorry if I offended you, you sound as if I did. I appreciate your input on this. I am aware that the sort method is for one-dimension arrays. That is why I took a two-dimention array and split it up in to two one-dimention arrays. And the recommendation is found in the link in the very first post of this thread. They (MSDN) posted an example and I take it as a recommendation as they are showing examples of sorting arrays as pairs and also using a class to reverse them. Although I never could get it to work. But I'm sure that know what they are doing, I just don't lol...

Your question about the fictional myArray(100,100) I'm not sure about, you say first or second element
Well elements, dimensions, whatever... Sometimes I interchange the two but it was pretty easy to tell what I was talking about there. Excuse me for that one. I try not to major in minor things ya know.

Not sure about your questions and purpose with this code.
Well, this is how I make my living John with this code. This is all I do; I'm a coder lol...

You can also implement your own QuickSort for multidimensions, but it will be an awful lot of swapping, Link To See
Yes this is how I have had this part of my code running for the last two years but I was trying to streamline it a little for speed as I am converting to the .net 2.0 (I am new to that). But the code examples that I got from you guys are a WHOLE lot better and more efficient. But I do appreciate you posting the link.

Thanks for all of your help.
 
There's something wrong with that link you posted, so I haven't seen the page. Anyway, for every little bit of the framework library there is given some sample code snippets, it's not their recommended way of doing everything... ;) IComparer is old .Net 1.1 general Object interface, IComparer(Of T) is new .Net 2.0 strongly typed generics interface, use the latter if you're doing .Net 2.0.
Well, this is how I make my living John with this code. This is all I do; I'm a coder lol...
That's nice to hear, although I don't see exactly how this relates to this particular sorting request. Did you review the collection list example? (post 5) Did it relate to anything around the source data or later usage of it?
 
Back
Top