Reputation: 2884
I have two record in the database with all the columns the same, the only columns which make difference between the two is a column called txnCo
. So the column which is inserted the next will have a higher value and I want to get the last one which is higher. In mysql I can use max function, but in Java I can not use math.max
since these are not numbers. So does anyone have any suggestion how to do it in the best way?
I am thinking of using the valueOf
function for each string and comparing the two.
Upvotes: 0
Views: 5257
Reputation: 4265
strA.compareTo(strB);
is what you are looking for.
If the value is < 0, the first string is lexicographically in front of the second. > 0, the second is in front. = 0, they are equal.
String.compareTo() in the Java API docs.
It is also described in the question How do I compare strings in Java?
Caution tho - it may not be defined in exactly the same way as your SQL comparison, depending on encodings, etc.
If you put all of your strings in any ordered collection (say a TreeSet
), you can then just extract the first/last as usual, the collections "know" about the natural order of the strings.
TreeSet<String> set = new TreeSet<>();
set.add("string1");
set.add("string2");
// ... //
String minString = set.first();
String maxString = set.last();
Upvotes: 2