Home > Back-end >  The sorting
The sorting

Time:04-17

To a string containing only lowercase letters and Numbers of the positive integer number sequence according to the Numbers in ascending order size, the relative position of the rest of the character remains the same, only change in the original number of substring, such as string as "aaa77xxxx88jj44", after the sorting result is "aaa44xxxx77jj88 non-numeric substring" aaa ", "XXXX" and "jj" relative position remains unchanged,
You need to consider:
(1) integer overflow, such as the Numbers for: "99999999999999999999999999999999", cannot be compared to firing a string into an integer;
(2) the first is 0 digital string processing, such as "00000000000000006" is less than the "7",
Written in Java sort algorithm implementation and output after sort of string,

How to put the string of Numbers and letters, respectively in the two arrays, and sort out where they are the same ah

CodePudding user response:

 
Public static void main (String [] args) {
String STR="aaa77xxxx88jj44kk003";

StringBuilder sb=new StringBuilder();
The StringBuilder sbNum=new StringBuilder ();
List List=new ArrayList<> (a);
For (char ch1: STR. ToCharArray ()) {
if (! IsNum (ch1)) {
If (sbNum. Length () & gt; 0 {
Sb. Append (" % s ");
List. The add (sbNum. ToString ());
SbNum. SetLength (0);
}
Sb. Append (ch1);
continue;
}
SbNum. Append (ch1);


}
If (sbNum. Length () & gt; 0 {
List. The add (sbNum. ToString ());
Sb. Append (" % s ");
SbNum. SetLength (0);
}

Map The map=new TreeMap<> (a);
Int start=0;
For (String str1: list) {
Start=0;
For (char ch: str1. ToCharArray ()) {
If (ch=='0') {
start++;
continue;
}
break;
}
The map. The put (str1. Substring (start), null);
}

System. The out. Println (sb. ToString ());
String format=the String. Format (sb. ToString (), new ArrayList<> (map) keySet ()). ToArray ());
System. The out. Println (format);
}

The static Boolean isNum (char ch) {
Return ch>='0' & amp; & Ch<='9';
}


Results;
aaa3xxxx44jj77kk88
  • Related