實現List按與一個字符串的相似度和字母順序排序(適用於模糊查詢后的排序)


     因公司業務需要,自己寫了一個,保存起來以后可能還會用到。如果還有更好的方法或者算法,希望大家提出來。

1、簡單的相似度算法(自己想到的)

     因為List中每個String都會包含一個標准的字符串,那么每個字符串除了標准字符串的左邊或者右邊都會有一定量的字符。相似度的定義是多出來的字符*100(只適用與在標准字符串左邊最多還多出99字符的字符串,可以根據情況擴大)+標准字符串左邊的字符的個數。例:標准字符串是"abc",那么字符串"abcd"與標准字符串的相似度是100,字符串"dabc"與標准字符串的相似度是101,字符串"xabcz"與標注字符串的相似度是201,字符串"xzabc"與標准字符串的相似度是202。因此這里定義的相似度最小為0,其越小就越接近標准字符串。說到這里這個就不應該就相似度、應該叫偏差度。

 1     private static Integer getSemblance(String toBeComparedString, String standardString){
 2         if(StringUtils.isBlank(toBeComparedString) || StringUtils.isBlank(standardString)){
 3             LOGGER.error("出錯,待比較的字符串或標注字符串為空");
 4             return null;
 5         }
 6         Integer diffIndex = toBeComparedString.length() - standardString.length();
 7         if(diffIndex<0){
 8             LOGGER.error("出錯,待比較的字符串比標准的字符串還要短");
 9             return null;
10         }
11         Integer headDiffIndex = toBeComparedString.indexOf(standardString);
12         if(headDiffIndex<0){
13             LOGGER.error("出錯,待比較的字符串不包含標准字符串");
14             return null;
15         }
16         return diffIndex*100+headDiffIndex;
17     }
相似度算法

 2、java中快速排序的方法(可以自定義排序規則)

      jave中的java.util.Comparator。Comparator<T>是一個快速排序的接口,只要寫個類實現接口的排序規則的方法就行。

 1 /**
 2  * File Name:Sort.java
 3  * Package Name:[packName]
 4  * Date:2015年11月12日上午10:57:14
 5  * Copyright (c) 2015年11月12日, Pwenlee All Rights Reserved.
 6  *
 7 */
 8 
 9 package com.olymtech.cs.openplatform.ds.service.impl;
10 
11 import java.util.Comparator;
12 
13 /**
14  * ClassName:Sort <br/>
15  * Function: 字符串compaer排序. <br/>
16  * Reason:     TODO ADD REASON. <br/>
17  * Date:     2015年11月12日 上午10:57:14 <br/>
18  * @author   PwenLee
19  * @version  
20  * @see      
21  */
22 public class Sort implements Comparator<String> {
23     
24     /**
25      * Function: 字符串排序的規則. <br/>
26      * @Date:    2015年11月12日 上午10:57:14 <br/>
27      * @author   PwenLee     
28      */
29     @Override
30     public int compare(String arg0, String arg1) {
31         if(arg0.compareTo(arg1)>0){
32             return 1;
33         }else if(arg0.compareTo(arg1)<0){
34             return -1;
35         }else{
36             return 0;
37         }
38     }
39 }
實現compare方法的類
1 //new后面是實現compare的類
2 Comparator<String> comparator = new Sort();
3 Collections.sort(List, comparator);
java.util.Comparator的用法

3、主算法

 1  1     public static List<String> sort(List<String> toBeComparedStringList, String standardString){
 2  2         List<String> listAfterSort = new ArrayList<String>();
         //定義待比較字符串List的相似度的List
         List<Integer> listSemblance = new ArrayList<Integer>();
         if(toBeComparedStringList.size()<=0){
             System.out.println("出錯,待比較的字符串為空");
             return null;
         }
         //初始化相似度的List
         for(int i=0;i<toBeComparedStringList.size();i++){
             listSemblance.add(getSemblance(toBeComparedStringList.get(i),standardString));
         }
         while(true){
             //循環的終止條件,當相似度的List里面全是Integer的最大值就跳出循環
             if((Integer)Collections.min(listSemblance).intValue() == Integer.MAX_VALUE.intValue()){
                 break;
             }
             else{
                 //取到相似度最小的位置
                 Integer minIndex = getMinIndex(listSemblance);
                 //並記錄初始的相似度
                 Integer origSemblance = listSemblance.get(minIndex);
                 //將這個位置的相似度置為Integer的最大值
                 listSemblance.set(minIndex, Integer.MAX_VALUE);
                 if(origSemblance.intValue() != listSemblance.get(getMinIndex(listSemblance)).intValue()){
                     //如果相似度List中沒有和初始相似度相同的
                     //則取出待排序List對應位置的字符串
                     String minString = toBeComparedStringList.get(minIndex);
                     listAfterSort.add(minString);
                 }else{
                     //如果相似度List中還有有和初始相似度相同的 則還原該位置相似度的初始值
                     listSemblance.set(minIndex, origSemblance);
                     List<String> tempList = new ArrayList<String>();
                     //將相似度List中的所有該相似度的位置記下,將這些位置的相似度置為Integer的最大值,並取出待排序List中對應的String
                     for(int i=0;i<listSemblance.size();i++){
                         if(listSemblance.get(i).intValue == origSemblance.intValue){
                             listSemblance.set(i, Integer.MAX_VALUE);
                            tempList.add(toBeComparedStringList.get(i));
                         }
                     }
                     //將相似度相同的臨時的List進行排序
                     Collections.sort(tempList, comparator);
                     listAfterSort.addAll(tempList);
                 }        
             }
         }
         return listAfterSort;
     }
排序算法

 

附件中是源碼,還包括了排序后截取自定義size的方法。

鏈接:http://pan.baidu.com/s/1hqpW8b6 密碼:6mn1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM