This article describes the Java implementation method of sorting in alphabetical order in upper and lower case letters. Share it for your reference, as follows:
The results you need to get in alphabetical order when sorting here. For example: a-----z...
import java.util.*;/** * Sort of uppercase and lowercase letters* @author Administrator * */public class z { //The above is sorted by the uppercase and the latter static Map<Character,Float> map=new HashMap<Character,Float>();//hashMap allows null values//Sorted by the uppercase and the former static Map<Character,Float> map1=new HashMap<Character,Float>();//hashMap allows null values// static{ Character ch; for (ch = 65; ch < 91; ch++) {//Uppercase 65.0; 66.0; 67.0; 68.0 map.put(ch,(float)ch.hashCode()); } for (ch = 97; ch < 123; ch++) {//Lowercase 64.5; 65.5 map.put(ch,(float)ch.hashCode()-(float)32-0.5f); } for (ch= 65; ch < 91; ch++) {//Uppercase 65.0; 66.0; 67.0; 68.0 map.put(ch, (float)ch.hashCode()); } for (ch = 97; ch < 123; ch++) {//Lowercase 64.5; 65.5 map.put(ch, (float)ch.hashCode()-(float)32+0.5f); } } /** * @first The array value after capitalization is found in the map in the value of the value of the value* @second Get the incrementing number through the sorting method of Collections List * @third Inverse operation of the map to obtain the char value* @param a Array to sort*/ public List addList(char[] ar){ List list =new java.util.ArrayList(); for (int i = 0; i < ar.length; i++) { list.add(map.get(ar[i])); } List lis=new ArrayList(); Collections.sort(list); Iterator it=list.iterator(); while(it.hasNext()){ String str=it.next().toString(); if(str.endsWith(".0")){ lis.add((char)Float.parseFloat(str)); }else{ lis.add((char)(Float.parseFloat(str)+0.5+32)); } } return lis; } /** * The array value in the previous capital is found in the map to find the value of the value* @param ar * @return */ public List addList1(char[] ar){ List list=new java.util.ArrayList(); for (int i = 0; i < ar.length; i++) { list.add(map.get(ar[i])); } List lis=new ArrayList(); Iterator it=list.iterator(); while(it.hasNext()){ String str=it.next().toString(); if(str.endsWith(".0")){ lis.add((char)Float.parseFloat(str)); }else{ lis.add((char)(Float.parseFloat(str)+0.5-32)); } } return lis; } public static void main(String []args){ System.out.println("Wulin.com test results: "); char ch [] ={'A','a','b','f','m','K'}; List list=new z().addList(ch); Iterator it=list.iterator(); while(it.hasNext()){ System.out.println(it.next()+","); } }}Running results:
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.