Browse Source

don't consider the length in StringFieldComparatorSource

sunyj 8 years ago
parent
commit
eac17ff3db

+ 7 - 26
mall-search/src/main/java/com/uas/search/sort/StringFieldComparatorSource.java

@@ -60,9 +60,8 @@ public class StringFieldComparatorSource extends FieldComparatorSource {
                         } else if (index1 > index2) {
                             return BEHIND;
                         } else {
-                            // 关键词在字符串中的位置相同时,比较字符串长度。长度较短,则排序靠前;长度相同时再按字符排序
-                            int compareLength = compareLength(str1, str2);
-                            return compareLength != PARALLEL ? compareLength : compareCharacter(str1, str2);
+                            // 关键词在字符串中的位置相同时,再按字符排序
+                            return compareCharacter(str1, str2);
                         }
                     } else {
                         return AHEAD;
@@ -72,11 +71,10 @@ public class StringFieldComparatorSource extends FieldComparatorSource {
                         return BEHIND;
                     } else {
                         // 均不包含关键词时
-                        // 如果之后没有其他排序,则比较字符串长度。长度较短,则排序靠前;长度相同时再按字符排序
+                        // 如果之后没有其他排序,再按字符排序
                         // 如果有其他排序,则这里排序优先级相等,由之后的排序再进行比较
                         if (!hasOtherSort) {
-                            int compareLength = compareLength(str1, str2);
-                            return compareLength != PARALLEL ? compareLength : compareCharacter(str1, str2);
+                            return compareCharacter(str1, str2);
                         } else {
                             return PARALLEL;
                         }
@@ -84,21 +82,6 @@ public class StringFieldComparatorSource extends FieldComparatorSource {
                 }
             }
 
-            /**
-             * 比较长度,长度较短,则排序靠前
-             */
-            private int compareLength(String str1, String str2) {
-                int length1 = str1.length();
-                int length2 = str2.length();
-                if (length1 < length2) {
-                    return AHEAD;
-                } else if (length1 > length2) {
-                    return BEHIND;
-                } else {
-                    return PARALLEL;
-                }
-            }
-
             /**
              * 比较字母顺序,优先级:字母 > 其他字符(优先级视为相等) > 数字
              */
@@ -108,7 +91,7 @@ public class StringFieldComparatorSource extends FieldComparatorSource {
                 for (int i = 0; i < array1.length; i++) {
                     char c1 = array1[i];
                     char c2 = array2[i];
-                    // 字母 > 其他字符(优先级视为相等) > 数字
+                    // 字母 > 数字 > 其他字符(优先级视为相等)
                     if (CharUtils.isCharacter(c1)) {
                         if (CharUtils.isCharacter(c2)) {
                             // 均为字母,值越小优先级越高
@@ -129,13 +112,11 @@ public class StringFieldComparatorSource extends FieldComparatorSource {
                                 return AHEAD;
                             }
                         } else {
-                            return BEHIND;
+                            return AHEAD;
                         }
                     } else {
-                        if (CharUtils.isCharacter(c2)) {
+                        if (CharUtils.isCharacter(c2) || CharUtils.isNumber(c2)) {
                             return BEHIND;
-                        } else if (CharUtils.isNumber(c2)) {
-                            return AHEAD;
                         }
                     }
                 }