一个JAVA中文排序的需求(记录一下)

需求:

       一个对象中有标题和作者字段,都是中文。要求给这个对象集合排序,排序规则为 按照对象当中的标题字段或者作者字段进行升序或者降序排序。

解决如下:

首先想了一下 java如何对中文进行排序如下代码:

package com.xc.test2;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortTest {

    private final static Comparator<Object> CHINA_COMPARE = Collator.getInstance(java.util.Locale.CHINA);

    public static void main(String[] args) {
        List<String> cityList = new ArrayList<String>();
        cityList.add("啊");
        cityList.add("的");
        cityList.add("吃");
        cityList.add("不");

        Collections.sort(cityList, CHINA_COMPARE);
        System.out.println(cityList);
    }

}

输出结果:

[啊, 不, 吃, 的]

果然可以根据汉子的拼音顺序排序,那就好办了

需求排序的实体类如下:

public class FullTextLibraryDTO  implements Serializable{

	private String id;	
	// 题名
	private String title;
	
	// 作者
	private String author;
	

	private String classfication;
	
	
	private ArrayList<String> indexTags;
	
	private String indexTag;
	
	private String press;
	
	// 创建时间
	private Date createDate;

	private String fileId;
	
	private Long tempSize;
	
	private String sortColum;
	
	private String sortType;

	

	public String getSortColum() {
		return sortColum;
	}

	public void setSortColum(String sortColum) {
		this.sortColum = sortColum;
	}

	public String getSortType() {
		return sortType;
	}

	public void setSortType(String sortType) {
		this.sortType = sortType;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getClassfication() {
		return classfication;
	}

	public void setClassfication(String classfication) {
		this.classfication = classfication;
	}

	public ArrayList<String> getIndexTags() {
		return indexTags;
	}

	public void setIndexTags(ArrayList<String> indexTags) {
		this.indexTags = indexTags;
	}

	public String getIndexTag() {
		return indexTag;
	}

	public void setIndexTag(String indexTag) {
		this.indexTag = indexTag;
	}

	public String getPress() {
		return press;
	}

	public void setPress(String press) {
		this.press = press;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public String getFileId() {
		return fileId;
	}

	public void setFileId(String fileId) {
		this.fileId = fileId;
	}

	public Long getTempSize() {
		return tempSize;
	}

	public void setTempSize(Long tempSize) {
		this.tempSize = tempSize;
	}
		
	
}

自己写了一个工具类如下(为写比较器做准备):

package com.bigdata.lab.ymlib.util;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
 * 
 * @author xuchuang
 *
 */
public class HanZiSortUtil {
	
	private final static Comparator<Object> CHINA_COMPARE = Collator.getInstance(java.util.Locale.CHINA);
	
	public static int compChineseWords(String word1,String word2){
		
		List<String> compList=new ArrayList<String>();
		compList.add(word1);
		compList.add(word2);
		Collections.sort(compList, CHINA_COMPARE);
		if(word1.equals(compList.get(0))){
			return 1;
		}else{
			return 0;
		}
		
		
	}

}

下面开始写比较器:

class FullTextLibraryDTOBytitle implements Comparator<FullTextLibraryDTO> {
	 
	@Override
	public int compare(FullTextLibraryDTO k1, FullTextLibraryDTO k2) {
 
		if(k1.getSortColum().equals("title")){
			
			if(k1.getSortType().equals("DESC")){
				if (HanZiSortUtil.compChineseWords(k1.getTitle(), k2.getTitle())==1) {
					return 1;
				} else if (HanZiSortUtil.compChineseWords(k1.getTitle(), k2.getTitle())==0) {
					return -1;
				} else {
					return 0;
				}
				
			}else{
				if (HanZiSortUtil.compChineseWords(k1.getTitle(), k2.getTitle())==1) {
					return -1;
				} else if (HanZiSortUtil.compChineseWords(k1.getTitle(), k2.getTitle())==0) {
					return 1;
				} else {
					return 0;
				}
				
			}
			
		}else{
			
			if(k1.getSortType().equals("DESC")){
				
				if (HanZiSortUtil.compChineseWords(k1.getAuthor(), k2.getAuthor())==1) {
					return 1;
				} else if (HanZiSortUtil.compChineseWords(k1.getAuthor(), k2.getAuthor())==0) {
					return -1;
				} else {
					return 0;
				}
				
			}else{
				if (HanZiSortUtil.compChineseWords(k1.getAuthor(), k2.getAuthor())==1) {
					return -1;
				} else if (HanZiSortUtil.compChineseWords(k1.getAuthor(), k2.getAuthor())==0) {
					return 1;
				} else {
					return 0;
				}
			}
			
		}
	
	}
 
}

比较器使用如下:

1.将要排序的字段  以及排序规则设置到dto当中

2.然后使用collections的sort方法

经测试,实现功能。

猜你喜欢

转载自blog.csdn.net/qq_34246546/article/details/81540646