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

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;
				}
			}
		});
	}


 

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现通讯录按名字首字母排序分组,可以按照以下步骤进行: 1. 获取通讯录数据,并将每个联系人的名字转换为拼音。 2. 根据拼音首字母对联系人进行分组。 3. 对每个分组内的联系人按照名字进行排序。 4. 在页面上渲染每个分组及其对应的联系人。 以下是一个简单的实现示例: 1. 使用 pinyin 库将联系人名字转换为拼音: ```js import pinyin from 'pinyin'; const contacts = [ { name: '张三', phone: '123456789' }, { name: '李四', phone: '987654321' }, // ... ]; const pinyinContacts = contacts.map(contact => { const pinyinName = pinyin(contact.name, { style: pinyin.STYLE_FIRST_LETTER }).join(''); return { ...contact, pinyinName }; }); ``` 2. 根据拼音首字母对联系人进行分组: ```js const groupedContacts = pinyinContacts.reduce((groups, contact) => { const firstLetter = contact.pinyinName[0].toUpperCase(); if (!groups[firstLetter]) { groups[firstLetter] = []; } groups[firstLetter].push(contact); return groups; }, {}); ``` 3. 对每个分组内的联系人按照名字进行排序: ```js Object.keys(groupedContacts).forEach(key => { groupedContacts[key].sort((a, b) => a.name.localeCompare(b.name)); }); ``` 4. 在页面上渲染每个分组及其对应的联系人: ```html <template> <div> <div v-for="(contacts, letter) in groupedContacts" :key="letter"> <h2>{{ letter }}</h2> <ul> <li v-for="contact in contacts" :key="contact.phone">{{ contact.name }}</li> </ul> </div> </div> </template> <script> export default { data() { return { groupedContacts: {}, }; }, mounted() { // 获取联系人数据并处理分组排序 // ... this.groupedContacts = groupedContacts; }, }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值