Sortwith

I might be misunderstanding but trying to use sort with to order a second array like this but the surfer order never changes

[code]Var surferorder() As Text = Array(“Surfer1”, “Surfer2”, “Surfer3”, “Surfer4”)
Var totals() As double = Array(Surfer1Total, Surfer2Total, Surfer3Total, Surfer4Total)

totals.Sort
surferorder.SortWith(totals)

MsgBox "Winner "+surferorder(surferorder.LastRowIndex)
[/code]

Try

totals.SortWith(surferorder)

No that doesn’t seem to do it either :frowning:

It definitely works on my side.
Maybe your surfers are already in the correct order.

Think of SortWith as ‘Sort As Well As’

[code]dim n() as integer =array(0,10,5,2,4)
dim s() as string = array(“zero”,“ten”,“five”,“2”,“four”)

//print the original data
print("unsorted “+join(s,”, "))
//sort using the values in n
n.sortwith(s)

print("sorted “+join(s,”, "))[/code]

Your n array needs to be a simple class such as integer, double or string(if you want it alphabetically)
Your s array can be anything, so long as the order of s is the same as n - i.e. they are added in the same (unsorted) order.

Output:

unsorted zero, ten, five, 2, four sorted zero, 2, four, five, ten

[quote=494743:@Martin Fitzgibbons]I might be misunderstanding but trying to use sort with to order a second array like this but the surfer order never changes

[code]Var surferorder() As Text = Array(“Surfer1”, “Surfer2”, “Surfer3”, “Surfer4”)
Var totals() As double = Array(Surfer1Total, Surfer2Total, Surfer3Total, Surfer4Total)

totals.Sort
surferorder.SortWith(totals)

MsgBox "Winner "+surferorder(surferorder.LastRowIndex)
[/code][/quote]
You’re calling it backwards. You need to use SortWith on the array you want to sort with:

totals.SortWith(surferOrder)
The reason is that SortWith can take more than one array so you can do multiple at the same time.

Probably not what I need. I thought I could sort the scores in into ascending order and then Sortwith would apply that same sorted order to the name array so the scores matched the names??

That’s exactly what this line of code is doing:

totals.SortWith(surferOrder)

There’s 2 issues with the original code:

  1. Remove:
    totals.Sort

  2. As noted above, change the SortWith line to:
    totals.SortWith(surferOrder)

SortWith sorts both together. By calling totals.Sort first, you break the matching order of the 2 arrays before calling SortWith.

Thanks Paul