欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java基础之字符串长度练习

程序员文章站 2022-04-06 15:07:49
...
package cn.itcast.p5.comparator;

import java.util.Comparator;

public class ComparatorByLength implements Comparator {

	@Override
	public int compare(Object o1, Object o2) {
		
		String s1 = (String)o1;
		String s2 = (String)o2;
		
		int temp = s1.length()-s2.length();
		
		return temp==0? s1.compareTo(s2): temp;
	}

}