Sort strings containing numbers: a clever way to sort numeric strings
Using System; using System. collections. generic; class Program {static void Main (string [] args) {string [] floors = {"3rd Floor", "2nd Floor", "11th floor"}; Array. sort <string> (floors, Factory. comparer); foreach (string s in floors) Console. writeLine (s); Console. readKey () ;}// Factory mode class Factory: IComparer <string> {private Factory () {} public static IComparer <string> Comparer {get {return new Factory () ;}} publ Ic int Compare (string x, string y) {return x. Length = y. Length? X. CompareTo (y): x. Length-y. Length ;}}
Write a method in java to sort the numbers in the string in ascending order (13a64bc52)
Package sort;
Import java. util. ArrayList; import java. util. List;
Public class StrSort {
Public static void main (String [] args ){
String str = new String ("13a64bc52 ");
Char [] strArray = str. toCharArray ();
List <Character> a = new ArrayList <Character> ();
For (int I = 0; I <strArray. length; I ++)
{
If (strArray [I] <= '9' & strArray [I]> = '0 ')
A. add (Character) strArray [I]);
}
Int [] integerArray = new int [a. size ()];
For (int I = 0; I <a. size (); I ++)
IntegerArray [I] = Character. digit (char) a. get (I), 10 );
IntegerArray = bubbingSort (integerArray );
For (int I = 0; I <integerArray. length; I ++)
System. out. print (integerArray [I]);
}
Public static int [] bubbingSort (int [] ){
Int swap = 0;
For (int I = a. length-1; I> = 0; I --)
For (int j = 0; j <I; j ++)
{
If (a [I] <a [j])
{
Swap = a [I];
A [I] = a [j];
A [j] = swap;
}
}
Return;
}
}
How to sort numeric strings?
Apply for an array with the same length as the original string array. The array type depends on the situation, preferably an integer.
Then, force type conversion is performed on the elements in the string in turn, and the elements are placed in the other array in turn. After sorting the other array, force conversion to the string. OK