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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Evaluating alpha/numeric values

Status
Not open for further replies.

ob1kanobee

Programmer
Dec 27, 2002
47
0
0
US
Is there a way to evaluate if one alpha/numeric value is greater than the other.

Example:
Canadian Postal Code A4A6B is greater than Canadian Postal Code A2A1B

I tried the eval method, but it returns an error.
 
Code:
<script type="text/javascript"><!--
var v1 = 'Canadian Postal Code A4A6B';
var v2 = 'Canadian Postal Code A2A1B';

alert(v1>v2);
//--></script>

as long as you understand the meaning of >, < and = in terms of characters.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
The problem with using that method is that if I have a numeric value such as 22222 and 555, it considers the first to be less than the second which is not true because it's comparing the data from left to right.
 
you'll need to use a test to see if the string is numeric. if it is, get the parsefloat value.

something like this:

Code:
<script type="text/javascript"><!--
var v1 = 'Canadian Postal Code A4A6B';
var v2 = 'Canadian Postal Code A2A1B';

if (!isNaN(v1) && !isNaN(v2)) {
    alert(parseFloat(v1) > parseFloat(v2));
} else {
    alert(v1 > v2);
}

//--></script>

however, i don't suggest using isNaN - it's the ghetto way of determining whether something is numeric.

that should get you started though.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I see no reason why cLFIaVa's original solution will not sort all Canadian postal codes as posed in the original question.

All Canadian postal codes have alterating letters and numerals, never two letters in a row and never two numerals in a row.

However, NEITHER solution will work for British postal codes since those codes may or may not have adjacent letters/numerals.

I also see that the question posed was about 'alpha/numeric' values, not pure alpha values nor pure numeric values. These pure values are easily sorted using the second method.

Alpha/numeric values that are in a regular format (like Canadian postal codes) are easily sorted via the first method unless there are pure numeric values mixed in. In that case the second method is required.

Regular format is defined as all numeric values within the a/n codes as being the same length and in the same positions through the code list. Anything else is irregular. A-12-XY-569 and X-95-TH-085 would make up a regular list, but if you add A-123-XY-569 or X-95-TH-85, the list becomes irrregular.

Values in an irregular format (British codes for example) can be sorted with either method above, but will give odd results most of the time, similar to the 222222 vs 555 syndrome (SW10 A55 will come before SW5 A55).

To sort mixed irregular alpha/numeric values so all values are alphabetically and numerically in order requires parsing each piece of the code, sorting it separately, and then concatenating the pieces back together. And if done wrong, this will really make a mess of the codes.

Even for something as simple as the British postal codes this can be a daunting experience. Woe unto anyone trying to do this with Ford part numbers or old style Borg Warner part numbers.


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
First, thanks to all who replied.
Since this project will also include global application, I will need to consider the points brought up by mmerlinn.
Sounds to me like a simple evaluation is not in order....bummer!
 
if you honestly need to sort differently based on country, you should simply be able to set up a case statement or if block to sort different based on the country at hand.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top