您的位置:首页 > 其它

关于通信录里面按照用户名称 排序(name的首字母、排序)

2013-09-02 19:40 357 查看
1.实体class

User

/**
* @Title: User.java
* @Package com.contact.miapsoft.entity
* @date 2013-8-24 9:17:51
* @version V1.0
*/
package com.contact.miapsoft.entity;
public class User {
private String UName;
private String phone;

public String getUName() {
return UName;
}
public void setUName(String uName) {
UName = uName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User [UName=" + UName + ", phone=" + phone + "]";
}
}

2.排序算法

private void order(List<User> userlist) {
Collections.sort(userlist, new Comparator<User>() {
public int compare(User arg0, User arg1) {
try {
byte[] buf1 = arg0.getUName().getBytes("unicode");
byte[] buf2 = arg1.getUName().getBytes("unicode");
int size = Math.min(buf1.length, buf2.length);
for (int i = 0; i < size; i++) {
if (buf1[i] < buf2[i])
return -1;
else if (buf1[i] > buf2[i])
return 1;
}
return buf1.length - buf2.length;
} catch (UnsupportedEncodingException ex) {
return 0;
}
}
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: