Although I've never implemented a sorting method *in excel VBA*, from
other programming languages, this should be simple. Not sure why VBA
would have a harder time with it ...
I'd do my sorting algorithm like this (for a real slow sort -- use a
better sort method please, i'm just being fast here)
Note: this may not be so different than what Libby wrote out in VBA
code directly. Try that first.
Code:
--------------------
dim array
array = list to be sorted
dim length as integer
length = length of array
dim X as integer, Y as integer
For X from 2 to length
Y = X
While (item Y of array is greater than item Y-1 of array) and (Y is greater than 1) do
Exchange Y with Y-1
decrement Y by 1
end While loop
if item Y is greater than 1 then
if item Y equals item Y-1 then
move item Y down the list, one spot at a time, until it is at item Length
decrement Length by one (so you don't try to sort this one again)
end if #2
end if #1
end For loop
--------------------
You'd end up, after this code, with a sorted list like this:
from 2532431
to
5432123
with the latter 2 and 3 in no order.
** edit: Actually, i'm lying here. You'd get 5432132, since each one
is placed in reverse order -- ie the first duplicate at the end, the
second duplicate at the (end-1) spot, etc.
If you want to sort that part of the list, then AFTER this sorting
algorithm, add another one (directly after the end For loop line)
Code:
--------------------
dim length2 as integer
length2=array.length
if length is not equal to length2 then
for X = (length+1) to (length2) do
Y = X
While (item Y of array is greater than item Y-1 of array) and (Y is greater than 1) do
Exchange Y with Y-1
decrement Y by 1
end While loop
end For loop
end If
--------------------
You'd end up now with
4325213
to
5432132
and
43235213
to
54321332
Not necessary of course if you don't care how the extras are sorted.
This method also has the convenient ability to 'cut' out the extras --
ie if the reason you want it sorted but dup's at the end is that you
don't want them, then it's easy to arrange, since the 'length' variable
is already correct for the length of your unique array, and you can
either just copy the original array to a new one using something like
for X from 1 to length do array2.X = array1.X
and it will stop when it gets to the end of the unique array (as long
as you don't end up losing your defined Length array) ... could also
probably just manually delete it, dunno how in VBA though.
-Joe