Java实现名字按拼音排序和多条件排序

Java实现名字按拼音排序和多条件排序

  1. 拼音排序
@Test
public void nameSortTest() {
	String [] nameArray = {"张三", "李四", "李华", "王五", "张伟", "陈晨"};
	List<String> nameList = new ArrayList<>(nameArray.length);
	Collections.addAll(nameList, nameArray);
	nameList = nameList.stream().sorted((name1, name2) -> {
		Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
		return cmp.compare(name1, name2);
	}).collect(Collectors.toList());
	System.out.println(nameList);
	// [陈晨, 李华, 李四, 王五, 张三, 张伟]
}
  1. 多条件排序
    实现User先按status排序,再按name, 最后按id排序
class User {
	private Integer id;
	private String name;
	private Boolean status;
    
	public User(Integer id, String name, Boolean status) {
		this.id = id;
		this.name = name;
		this.status = status;
	}
    
	public Integer getId() {
		return id;
	}
    
	public void setId(Integer id) {
		this.id = id;
	}
    
	public String getName() {
		return name;
	}
    
	public void setName(String name) {
		this.name = name;
	}
    
	public Boolean getStatus() {
    	return status;
	}
    
	public void setStatus(Boolean status) {
		this.status = status;
	}
    
	@Override
	public String toString() {
		return "User{" +
				", status=" + status +
				", name='" + name + '\'' +
				", id=" + id +
				'}' +"\n";
	}
}
@Test
public void multipleConditionSortTest() {
    
    String [] nameArray = {"张三", "李四", "李华", "王五", "张伟", "陈晨", "王五"};
	List<User> userList = new ArrayList<>(nameArray.length);
	for (int i = 0; i < nameArray.length; i++) {
		userList.add(new User(i,nameArray[i], i % 3 == 0 ));
	}
    userList = userList.stream().sorted((user1, user2) -> {
            
    	if (!user1.getStatus().equals(user2.getStatus())) {
   			return user1.getStatus().compareTo(user2.getStatus());
   		} else if (!user1.getName().equals(user2.getName())) {
   			Comparator cmp = Collator.getInstance(java.util.Locale.CHINA);
            return cmp.compare(user1.getName(), user2.getName());
        } else  {
            user1.getId().compareTo(user2.getId());
        }
                
    }).collect(Collectors.toList());
	System.out.println(userList);
    // [User{, status=false, name='陈晨', id=5}
	// ,User{, status=false, name='李华', id=2}
	// ,User{, status=false, name='李四', id=1}
	// ,User{, status=false, name='张伟', id=4}
	// ,User{, status=true, name='王五', id=3}
	// ,User{, status=true, name='王五', id=6}
	// ,User{, status=true, name='张三', id=0}
	// ]
}

顺便总结几种Array转list的方法

String [] nameArray = {"张三", "李四", "李华", "王五", "张伟", "陈晨"};
List<String> nameList = new ArrayList<>(nameArray.length);
for (String name: nameArray) {
    nameList.add(name);
}
String [] nameArray = {"张三", "李四", "李华", "王五", "张伟", "陈晨"};
List<String> nameList = new ArrayList<>(Arrays.asList(nameArray));
String [] nameArray = {"张三", "李四", "李华", "王五", "张伟", "陈晨"};
List<String> nameList = Arrays.stream(nameArray).collect(Collectors.toList());
String [] nameArray = {"张三", "李四", "李华", "王五", "张伟", "陈晨"};
List<String> nameList = new ArrayList<>(nameArray.length);
Collections.addAll(nameList, nameArray);

你可能感兴趣的:(java,java,sort,拼音,compare,list)