實體類集合(List)根據實體類的一個或者多個屬性對集合進行排序-Collections.sort

Collection.sort(userList, new Comparator<user>(){
	@Override
	public int compare(User u1, User u2){
		int ct=0;
		if(u2 == null){
			ct = -1;
		}
		if(ct == 0){
			String userId = u1.getUserId();
			String o_userId = u2.getUserId();
			if(userId == null){
				userId = "";
			}
			if(o_userId == null){
				o_userId = "";
			}

			ct = userId.compareTo(o_userId);
			if(ct == 0){
				String id = u1.getId();
				String o_id = u2.getId();
				if( id == null){
					id = "";
				}
				if(o_id == null){
					o_id = "";
				}
				ct = id.compareTo(o_id);
			}
		}
		return ct;
	
	}
});

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章