Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alphanumeric sorting

Status
Not open for further replies.

bjrollet

Programmer
Apr 10, 2002
118
US
Het all,

I have a one dimension array that conatains alphanumeric strings -
Code:
tmpArray(0)="1-0-1"
tmpArray(1)="10-0-0"
tmpArray(2)="7-0-1"
...

How can a sort them so they are in order?
Code:
tmpArray(0)="1-0-1"
tmpArray(1)="7-0-1"
tmpArray(2)="10-0-0"


Any good sorting algorithims out there? This working out thing isn't working out.
 
The problem is this. The strings may contain a letter, ex. "10-65A-0" This working out thing isn't working out.
 
Is the letter a Hex digit? If so, you can do as DangerPowers suggests, but convert from hex first.

Otherwise:
You will need to set up an 2D array with your array split on '-'. Then sort new array (shellsort, bubblesort whatever, depending on size of array etc) on its first field Let me know if this helps
________________________________________________________________
If you are worried about how to post, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 

I may have an answer for you but before I post this terrible looking code I would like to ask you...

If you had the input of

tmpArray(0) = "1-0-1"
tmpArray(1) = "10-0-0"
tmpArray(2) = "7-0-1"
tmpArray(3) = "10-65A-0"
tmpArray(4) = "2-4-1"
tmpArray(5) = "10A-151-32"
tmpArray(6) = "32-65B-4"
tmpArray(7) = "9-65A-0"
tmpArray(8) = "10a-151-32"
tmpArray(9) = "10B-151-32"
tmpArray(10) = "151-69-72"
tmpArray(11) = "5-34-15A"
tmpArray(12) = "32-65A-5"

is the ouput that you are looking for something like...

1-0-1
2-4-1
5-34-15A
7-0-1
9-65A-0
10-0-0
10-65A-0
32-65A-5
32-65B-4
151-69-72
10a-151-32
10A-151-32
10B-151-32
 
This is more to the tune...

1-0-1
2-4-1
5-34-15A
7-0-1
9-65A-0
10-0-0
10-65A-0
10a-151-32
10A-151-32
10B-151-32
32-65A-5
32-65B-4
151-69-72
This working out thing isn't working out.
 

I have a solution for you but I have not written code this ugly in a couple of years so I will explain how I was able to solve this problem and let you do your own coding.

I used the val function on the first set of numbers with a bubble sort. This puts the array in order by number on the first section.

Then I checked for duplicate entries using the val function with two loops (one inside the other like so).

For A = lBound(Array) to uBound(Array)
For B = A + 1 To uBound(Array)
If Val(Array(A)) = Val(Array(B)) Then
'I found two entries that match

I then used
Variable1 = Trim(Str(Val(Array(A))))
Variable2 = Trim(Str(Val(Array(B))))

and checked the length of each variable vs its origional part, Ex.

Array(A) = "10"
Array(B) = "10A"

The length of the variable that contained the Trim(Str(Val(Array(B)))) would not be equal to the origional length.

&quot;10&quot; <> &quot;10A&quot;

Then for those entries that were equal to each other (&quot;10A&quot; = &quot;10A&quot;) I went to the next section first checking to see if a number was larger than the first and if they equaled then checked for ascii characters and if they equaled then went to the 3rd section and repeated the process.

GOOD LUCK!!!

My Code is nasty!

 
If you are VB6 there is, I am told, a split function that will make it easy, if VB5 one has to write one....

after that split the lines by the &quot;-&quot; sign in to three, prepend zeros as in

srrng = right$( &quot;00000&quot; & strng, 6)

recomppse, sort.

Decompse again, strip out leading zeros
strng = trim$(str$( val (strng)))

OOps, you had some trailing alphas...

while left$(strng, 1) = &quot;0&quot;
strng=mid$(strng, 2)
wend

and recompse again, and walla (voila).

Thats what functions are for, the mess can be buried in one call.

If you are going to do it in spahghetti code you probbably have a few days of billing.

 
even if in some earlier ver, there is a 'basSplit' function in either VB or Ms. A forums which will do the split part, see thread181-32062 for ONE such implementation.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
bjrollet - In your This is more to the tune.., post, you showed a list of items which are correctly sorted. In this list are the following two lines: Please tell us that there is a typo in your list.

10a-151-32
10A-151-32

Or does 10a come before 10A - ie lower case letters are to come before upper case letters?

Also, you should be aware that jnicks's algorithm may give you some trouble. Consider the following two items from your list:

10B-151-32
32-65A-5

I fear that these two would be sorted differently using that algorithm. (000032 will come before 00010B)

Before you can use any sort algorithm, you are going to have standardize the layout of your data, and given the samples that you have shown, implies that you not only must split off the first part of the string, but you will also have to separate the numeric portion from the alpha portion.

Will these value always start with a digit?
What is the max number of letters that can appear in one of these keys? Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top