📄 bubblesorter.java
字号:
package examples.i18n;
import java.util.Locale;
import java.text.Collator;
import java.text.CollationKey;
/** A class to demonstrate the use of Collator
* and CollationKey classes
*/
public class BubbleSorter {
/** main entrypoint - starts the application
* @param args not used
*/
public static void main( String[] args ) {
String[] data = { "Washington", "Cairo",
"paris", "london",
"Ottawa", "Madrid" };
System.out.println( "Before:" );
for( int i = 0; i < data.length; i++ ) {
System.out.println( "\t" + data[i] );
}
sort( data );
System.out.println( "After:" );
for( int i = 0; i < data.length; i++ ) {
System.out.println( "\t" + data[i] );
}
}
/** Sort the input array of strings
* @param input the array to be sorted
*/
public static void sort( String[] input ) {
// get collator for the default locale
Collator c
= Collator.getInstance( Locale.CANADA_FRENCH );
// build up the array of collation keys
CollationKey[] ckeys
= new CollationKey[input.length];
for ( int i = 0; i < input.length; i++ ) {
ckeys[i] = c.getCollationKey( input[i] );
}
// do the bubble sort
CollationKey tempKey;
for ( int i = 1; i < ckeys.length; i++ ) {
for ( int j = 0; j < ckeys.length-i; j++ ) {
if ( ckeys[j].compareTo( ckeys[j+1] )
> 0 ) {
// swap values
tempKey = ckeys[j];
ckeys[j] = ckeys[j+1];
ckeys[j+1] = tempKey;
}
}
}
// recover the sorted array from the sorted
// CollationKey objects
for ( int i = 0; i < input.length; i++ ) {
input[i] = ckeys[i].getSourceString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -