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!

compareTo method

Status
Not open for further replies.

ob1kanobee

Programmer
Dec 27, 2002
47
0
0
US
Is there a similar method in javascript like there is in java for the compareTo method?:
sVariable1.compareTo(sVariable2);
 
Or, to test absolute equality (value [!]and[/!] type equality, rather than just value equality), then you can use three equals signs in a row:

Code:
if (string1 ==[!]=[/!] string2) {
   // do something
}

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I'll be using this method for verifying canadian postal codes. So I need to know when Postal Code "A1A5K9" is greater value than "A1A5J4".
The compareTo method returns a true if the object being compared to is of greater value.
Since I'm fairly new with javascript, I didn't know if value1 > value2 would always work, even with alpha and alpha/numeric values.
 
This is an instance where all I would do is run some tests and tell you what I learned. You should do that instead.

Create something with two text boxes and a button which, when clicked, does the comparison and uses an alert-message to pop up the result.

if(message value as expected)
try another test
else
debug (of post code to tek-tips and ask for debugging advice)

Once you've debugged and determined your final "compareTo" algorithm, ask someone else to test it. The first test they try will differ from all the ones you just did. You'll find a bug and then it's back to step 1... but you'll be closer!

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
No problemo!

But if you come up with your solution, I hope you post it for future Tek-Tippers who are interested in the same thing!

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top