Java CompareTo no arroja buenos resultados

I've implemented a function to check if one url is newer that the other one, in order to go through whole website ex: page1, page2, page3.

But when i'm using compareTo function in Java it doesn't return good result in some case check it out and Please help!

String someString1 = "http://view-inventory.aspx?_page=9";
String someString2 = "http://view-inventory.aspx?_page=11";

int comparisonResult = someString2.compareTo(someString1);

System.out.println("Comparison result:"+comparisonResult);

    System.out.println("We're equal");
else if(comparisonResult>0){
    System.out.println("Some one is bigger");
else {
    System.out.println("Some one is smaller");

when i compare page=8 and page=9 it works fine but for example above(page=9 and page=10) it doesn't work :(

preguntado el 09 de septiembre de 13 a las 22:09

Why do you mean by doesn't work ? -

Ordenamiento lexicográfico... (Strikes Again) -

3 Respuestas

Eso es porque 1 (the first digit of 11) is smaller than 9. Si utiliza 09 y 11 it will work. In order to make it work, extract the number and compare the number by comparing integers, instead of characters.

Respondido el 09 de Septiembre de 13 a las 22:09

That is because 9 is bigger than 1.

You need to substring the part after "=" and cast it to int, then compare.

Respondido el 09 de Septiembre de 13 a las 22:09

Simple stuff. 9 > 1. Parse the URL until =. Cast that char to Int. Then, do the same thing. you'd get your desired output!

Respondido el 09 de Septiembre de 13 a las 22:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.