---
title: "按字母排序"
author: "yiyickf"
date: "2011-04-11T13:36:12Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-04-11T13:36:12Z"
id: 997447
url: "https://yiyickf.iteye.com/blog/997447"
---

对字符串进行排序，用任意一种编程语言来实现，不能使用现有的类，在排序中，字符串“Bc”，“Ad”，“aC”,“Hello”，“Xman”，“little”，“During”,“day”能够排序成“Ad”，"aC"，“Bc”，“During”，“day”，“Hello”，“little”，“Hello”，也就是说，在排序的过程并不是传统的按照字符串排序，在排序中还需要将小写字母一并排序，也就是说a字符串要在B或b之前。

 

```
import java.util.Arrays;
import java.util.Comparator;

public class Test {
public static void main(String[] args) {

	String[] strs={"Bc","Ad","aC","Hello","Xman","little","During","day"};
	Arrays.sort(strs, new Comparator<String>(){
		int cha='a'-'A';
		public int compare(String o1, String o2) {
			char[] so1=o1.toCharArray();
			char[] so2=o2.toCharArray();
			int min=so1.length<so2.length?so1.length:so2.length;
			for(int i=0;i<min;i++){
				if(so1[i]!=so2[i]){
					if(so1[i]-so2[i]==-cha || so1[i]-so2[i]==cha){
						return so1[i]-so2[i];
					}else{
						int so1_1=so1[i];
						int so2_1=so2[i];
						if(so1[i]<='Z'){
							so1_1+=cha;
						}
						if(so2[i]<='Z'){
							so2_1+=cha;
						}
						return so1_1-so2_1;	
					}
				}
			}
			return so1.length-so2.length;	
		}
	});
	
	System.out.println(Arrays.toString(strs));
	
}
}
```