1. String Array sorting:
String [] reallist =...
// Sort by alphabet
Alphabetcompare AC = new alphabetcompare ();
Arrays. Sort (reallist, AC );
Public class alphabetcompare implements comparator <string> {
@ Override
Public int compare (string str1, string str2 ){
Return str1.compareto (str2 );
}
}
Very simple !!
2. In order to sort the obtained names in alphabetical order, write the following class:
JavaCode
- Public ClassAlphabetcomparatorImplementsComparator <string> {
-
-
- PrivateRulebasedcollator collator;
-
-
- PublicAlphabetcomparator (){
-
- Collator = (rulebasedcollator) collator
-
- . Getinstance (Java. util. locale. China );
- }
-
-
- @ Override
-
- Public IntCompare (string obj1, string obj2 ){
-
-
- Collationkey C1 = collator. getcollationkey (obj1 );
-
- Collationkey C2 = collator. getcollationkey (obj2 );
-
- ReturnCollator. Compare (collationkey) C1). getsourcestring (),
-
- (Collationkey) C2). getsourcestring ());
-
- }
-
- }
This is in ascending order. Changing C1 and C2 is in descending order.
Call method Java code
- Alphabetcomparator CFC =NewAlphabetcomparator ();
- Collections. Sort ([data to be sorted], CFC)