2012-12-12 58 views
0

我有一組字符串,它們表示產品大小,其中大多數字符的含義都是重複的,但沒有名稱。 (IE大小至少有14種不同的拼寫可能,每個拼寫都需要保留。)我需要根據它們代表的大小對它們進行排序。任何可能的小值應該出現在任何可能的中間值等按任意順序對字符串進行排序

我認爲這是可能是實現其包含不同的設置上它代表的底座尺寸大小的每個分組的特定比較的唯一途徑。然後,我可以通過確定特定大小落入哪個Set來實現-1,0,1關係。

有沒有更強大的方法來實現這一目標?具體來說,我擔心從現在起2周後,有人想出另一種方式來拼大型。

編輯:要清楚其不是實際的比較我有一個問題,其用含有每個組的組設置。這是處理這種情況的正常方法嗎?我如何進一步證明它,因此每個新增規模都不需要完全重新編譯/部署?

+0

@downvote:請讓我知道這個問題的問題是什麼,我會重新考慮 – thedan

+0

我認爲這可能有助於給出更具體的例子你的數據是什麼樣子。 –

回答

1

定製比較溶液。我不明白你爲什麼擔心這不夠健壯。

+0

它不是使用我擔心的比較器,它處理每個項目組。爲了清晰起見添加編輯 – thedan

0

要在字符串(或對象一般)的集合強加一個任意排序,該標準意味着要做到這一點,你認爲實施比較。

除「手動」解決方案,你建議,你可以考慮串的相對edit distance比較典型的例子。這將是更多的靈活在這個意義上說,它將工作在你沒有想到的替代方案。但就涉及的工作而言,對您的應用程序來說可能是過度的。

+0

感謝您的鏈接,我一定會檢查出來。這確實看起來像過度殺傷。 – thedan

1

一種簡單的方法將是從資源加載大小別名。一些示例代碼(將所有文件都在同一個包):

的接口來封裝大小屬性

public interface Sized { 
    public String getSize(); 
} 

的產品類

public class Product implements Sized { 

    private final String size; 

    public Product(String size) { 
     this.size = size; 
    } 

    public String getSize() { 
     return size; 
    } 

    @Override 
    public String toString() { 
     return size; 
    } 
} 

,做魔術比較器:

import java.util.Comparator; 
import java.util.HashMap; 
import java.util.Map; 
import java.util.ResourceBundle; 

public class SizedComparator implements Comparator<Sized> { 

    // maps size aliases to canonical sizes 
    private static final Map<String, String> sizes = new HashMap<String, String>(); 

    static { 
     // create the lookup map from a resourcebundle 
     ResourceBundle sizesBundle = ResourceBundle 
       .getBundle(SizedComparator.class.getName()); 
     for (String canonicalSize : sizesBundle.keySet()) { 
      String[] aliases = sizesBundle.getString(canonicalSize).split(","); 
      for (String alias : aliases) { 
       sizes.put(alias, canonicalSize); 
      } 
     } 
    } 

    @Override 
    public int compare(Sized s1, Sized s2) { 
     int result; 
     String c1 = getCanonicalSize(s1); 
     String c2 = getCanonicalSize(s2); 
     if (c1 == null && c2 == null) { 
      result = 0; 
     } else if (c1 == null) { 
      result = -1; 
     } else if (c2 == null) { 
      result = 1; 
     } else { 
      result = c1.compareTo(c2); 
     } 
     return result; 
    } 

    private String getCanonicalSize(Sized s1) { 
     String result = null; 
     if (s1 != null && s1.getSize() != null) { 
      result = sizes.get(s1.getSize()); 
     } 
     return result; 
    } 

} 

SizedComparator.properties:

1 = Small,tiny 
2 = medium,Average 
3 = Large,big,HUGE 

單元測試(只爲快樂流量):

import org.junit.Before; 
import org.junit.Test; 

public class FieldSortTest { 

    private static final String SMALL = "tiny"; 
    private static final String LARGE = "Large"; 
    private static final String MEDIUM = "medium"; 

    private Comparator<Sized> instance; 

    @Before 
    public void setup() { 
     instance = new SizedComparator(); 
    } 

    @Test 
    public void testHappy() { 
     List<Product> products = new ArrayList<Product>(); 
     products.add(new Product(MEDIUM)); 
     products.add(new Product(LARGE)); 
     products.add(new Product(SMALL)); 

     Collections.sort(products, instance); 

     Assert.assertSame(SMALL, products.get(0).getSize()); 
     Assert.assertSame(MEDIUM, products.get(1).getSize()); 
     Assert.assertSame(LARGE, products.get(2).getSize()); 
    } 
} 

注意把ResourceBundle被自動緩存。您可以通過編程方式重新加載資源包:

ResourceBundle.clearCache(); 

(自從Java 1.6)。或者,您可以使用一些Spring magic來創建自動重新加載消息資源。

如果從搖搖晃晃的屬性文件閱讀是不夠的,你冷靜可以很容易讓你的尺寸的別名在數據庫了。

+0

這裏有幾個有趣的想法。感謝您花時間嘲笑它。 – thedan

+0

它實際上也是工作代碼( - : –

相關問題