代码实现:字符串排序

import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

//字符串排序
public class Test {
	public static void main(String[] args) {
		TreeSet<Character> ts = new TreeSet<>(new Comparator<Character>() {

			@Override
			public int compare(Character c1, Character c2) {
				int num = c1 -c2 ; 
				return num == 0 ? 1 : num;
			}
		});
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入字符串:");
		String s = sc.nextLine();
		char[] chs = s.toCharArray();
		for (char c : chs) {
			ts.add(c);
		}
		for (char c : ts) {
			
			System.out.print(c);
		}
	}
}

 

posted on 2017-03-09 20:06  LoaderMan  阅读(316)  评论(0编辑  收藏  举报

导航