-5

I have few strings whom I have to compare and the print the smallest,For eg:

A0< A1

A12< A22

A< B

12<23

a12< a21

Note:The string could be only number or only alphabet or can be mixture of both we have to compare it and print the smallest one first. My procedure:I checked the first character of string by "charAt " if its a letter I used compare to function if its a number I used IntParse method. But I could not figure out how to compare alphanumerical string like A12 < A21.

if ((assignment.getAssigncode()[i]).matches("[a-zA-Z]") && 
   ((assignment.getAssigncode()[j]).matches("[a-zA-Z]"))) {
   if ((assignment.getAssigncode()[i]).compareTo((assignment.getAssigncode()[j])) > 0) {
      swap();
   } else {
       if (Integer.parseInt((assignment.getAssigncode()[i])) > 
           Integer.parseInt((assignment.getAssigncode()[j]))) {
           swap();
       }
   }
}
Änsu Man
  • 3
  • 6
  • `System.out.println(Collections.min(collectionOfStrings))`? https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#min-java.util.Collection- – JB Nizet Sep 02 '17 at 10:33
  • show code, its better way of communication tha 'story' – Jacek Cz Sep 02 '17 at 10:34
  • 6
    Not that question again. Someone else asked a similar one and couldn't explain why `A12` should be larger than `A22`, but `12` is smaller than `23`. Please explain your rules. (I mean this question: [Sorting of two strings character by character in JAVA?](https://stackoverflow.com/questions/45896117/sorting-of-two-strings-character-by-character-in-java)) – Tom Sep 02 '17 at 10:37
  • 1
    *"A12>A22"* and *"But I could not figure out how to compare alphanumerical string like A12 < A21."* So `A12` is > `A22` but < `A21`? These are some pretty bizarre rules. – T.J. Crowder Sep 02 '17 at 10:42
  • 3
    @Tom: Same person, different account, almost certainly, given the account names "Änsu Man" and "Ansu" – T.J. Crowder Sep 02 '17 at 10:43
  • Lol, this is a sock puppet account and the other question I've linked was yours ... – Tom Sep 02 '17 at 10:44
  • Because its the demand of question,You have to print A12 before A22 you are compairing it lexiographically. – Änsu Man Sep 02 '17 at 10:44
  • @T.J.Crowder Yeah, I also noticed that a few moments ago ^^. I wonder if OP tried to bypass the question limit of the older account? – Tom Sep 02 '17 at 10:44
  • If you print A12 ___before___ A22, then you obviously have to handle A12 to be ___smaller___ than A22. – Tom Sep 02 '17 at 10:45
  • If you could not answer its ok – Änsu Man Sep 02 '17 at 10:45
  • @ÄnsuMan: *"Because its the demand of question,You have to print A12 before A22 you are compairing it lexiographically"* Yes. But your question says A12 should be > A22 (but that A12 should be < A21, which just makes no sense at all). – T.J. Crowder Sep 02 '17 at 10:45
  • Yes we have to do it that way – Änsu Man Sep 02 '17 at 10:47
  • I edited that mistake its A12 – Änsu Man Sep 02 '17 at 10:48
  • Use the [String.compareTo()](https://www.tutorialspoint.com/java/java_string_compareto.htm) method. – DevilsHnd - 退職した Sep 02 '17 at 11:18

1 Answers1

0

First we have to check that the first character of string is a Alphabet or not using 'charAt(0)' function,if so then we have to compare string1 charAt(0) and string2 'charAt(0)' if string1charAt(0) is less then we could swap,else if both the first character are equal the we could use substring(1) method to convert the remaining string to integer and then we can directly compare it.

       if (((assignment.getAssigncode()[i]).charAt(0)) >= 65 && ((assignment.getAssigncode()[i]).charAt(0)) <= 122 && ((assignment.getAssigncode()[j]).charAt(0)) >= 65 && ((assignment.getAssigncode()[j]).charAt(0)) <= 122) {
 if (((assignment.getAssigncode()[i]).charAt(0)) > ((assignment.getAssigncode()[j]).charAt(0))) {
swap();}
}
else if (((assignment.getAssigncode()[i]).charAt(0)) == ((assignment.getAssigncode()[j]).charAt(0))) {
                                                int y = Integer.parseInt((assignment.getAssigncode()[i]).substring(1));
                                                int v = Integer.parseInt((assignment.getAssigncode()[j]).substring(1));
                                                if (y > v) {
swap();
}
}

  else {
                                                if (Integer.parseInt((assignment.getAssigncode()[i])) > Integer.parseInt((assignment.getAssigncode()[j]))) {
swap();
}
Änsu Man
  • 3
  • 6