Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi..
I am trying to compare two string for sorting purpose.i am comparing this string directly by using =,> and < operator.i am getting problem in some case eg:

if(abc12<abc2)
> it returns true but actual i want abc12 string must be greater.

my code is

Java
str1=abc12;
str2=abc2;

if(str1<str2)
{
 alert('str1');
}
else
{
alert('str2');
}


i got output as str1. Actually i want str2.
Provide a solution.
Posted
Updated 10-Sep-12 2:14am
v3

first of all you should look at the function string.localCompare[^] as well as to make it work properly you will have to compare string length too
 
Share this answer
 
hiii,

C#
function myFunction()
{
var x="abc123";
var y="abc122"
if((x.localeCompare(y)<0 && x.length<=y.length))
{
alert(y);
}
else
{
alert(x);
}
}



hope this will work fine
 
Share this answer
 
Hi,

try like this,

JavaScript
str1 = 'abc1';
str2 = 'abc2';

if(str1 < str2)
{
  alert('str1');
}
else
{
  alert('str2');
}


hope it works.
 
Share this answer
 
Try this:
JavaScript
str1=abc12;
str2=abc2;
 
if(str1.length<str2.length)>
{
 alert('str1');
}
else
{
alert('str2');
}
 
Share this answer
 
Comments
prashant patil 4987 10-Sep-12 8:48am    
hey Vijay,
What you want to do...
?
are you want to compare length or whole string character by character?
tell me?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900