2011-09-06 18 views
5

我正在閱讀.csv文件,有點像excel中的電子表格。有一定數量的列,由文件決定,我使用.split(",")方法將每行讀入一個字符串數組。然後我把它放到一個數組列表中,這樣它可以保存所有的字符串數組而不給它一個特定的大小。但是,當我使用Collections.sort()對數組列表進行排序時,程序中斷。問題是什麼?這裏是我的代碼進行排序:對字符串[]數組的列表排序

Collections.sort(stringList, new Comparator <String[]>() { 
    public int compare(String[] strings, String[] otherStrings) { 
     return -1 * (strings[sortNum].compareTo(otherStrings[sortNum])); 
    } 
}); 
+1

'sortNum'從哪裏來? –

+2

「休息」是什麼意思?它在哪裏不破?你會得到什麼錯誤?你期望會發生什麼?每行都有'sortNum' + 1個單元格嗎? –

+0

什麼是'sortNum'? – guardianpt

回答

5

兩點:

  • 不要乘以-1的compare結果扭轉了比較。 Integer.MIN_VALUE * -1仍然是Integer.MIN_VALUE。相反,顛倒比較本身的順序
  • 我的猜測是你已經實際上得到了一些行沒有足夠的列。也許你應該把這些放在最後?

喜歡的東西:

Collections.sort(stringList, new Comparator <String[]>() { 
    public int compare(String[] x1, String[] x2) { 
     if (x1.length > sortNum && x2.length > sortNum) { 
      return x2[sortNum].compareTo(x1[sortNum]); 
     } 
     if (x1.length > sortNum) { 
      return 1; 
     } 
     if (x2.length > sortNum) { 
      return -1; 
     } 
     return x2.length - x1.length; 
    } 
}); 

另外,第一過濾列表,使絕對確保的所有行有足夠的列。

+0

這工作很好。謝謝 – nathpilland

1

好,無論是串[sortNum]或otherStrings [sortNum]可能是出界。你需要做一些檢查來防止這種情況發生。此外,字符串[sortNum]或otherStrings [sortNum]可能爲空。我敢打賭你碰到了這兩件事之一。調用堆棧指示什麼?

0

我懷疑你可能有一個關於'sortNum'變量的關閉問題。有關指導,請參閱Jon Skeet's closure article,即使它處理C#中的閉包,它仍應該是相關的。即使你沒有這個問題,這是一個很好的閱讀。 :)

0

您可以爲空 「細胞」 的默認值:

  public int compare(String[] strings, String[] otherStrings) { 
       String one, other; 
       one = other = ""; // default value 
       if (sortNum<strings.length && strings[sortNum] != null) { 
        one = strings[sortNum]; 
       } 
       if (sortNum<otherStrings.length && otherStrings[sortNum] != null) { 
        other = otherStrings[sortNum]; 
       } 
       return -1 * (one.compareTo(other)); 
      } 
1

嘗試使用此

一類比較有構造函數:

public class MyStringArrayComparator implements Comparator<String[]>{ 

     Integer sortNum; 

     public MyStringComparator(Integer index) { 
       sortNum = index; 
     } 

     @Override 
     public int compare(String[] strings, String[] otherStrings) { 
       return -1*(strings[sortNum].compareTo(otherStrings[sortNum])); 
     } 
} 

,並在你的代碼

Collections.sort(stringList,new MyStringArrayComparator<String[]>(index)); 

希望對你有用

0

共享代碼,以防有人需要對多列進行排序。

public final class ArrayComparatorWithIndex<T extends Comparable<T>> implements Comparator<T[]> 
{ 
    private final int[] indexToSort; 

    public ArrayComparatorWitIndex(int[] indexToSort) 
    {   
     if(indexToSort == null || indexToSort.length == 0){ 
      throw new IllegalArgumentException("Index to use for sorting cannot be null or empty."); 
     } 
     this.indexToSort = indexToSort; 
    } 

    @Override 
    public int compare(T[] str, T[] otherStr) 
    { 
     int result= 0; 
     for (int index : indexToSort) 
     { 
      result= str[index].compareTo(otherStr[index]); 
      if (result != 0){ 
       break; 
      } 
     } 
     return result; 
    } 
} 

//Example how to use it: 
int[] indexForSorting= new int[] { 1, 3 }; 
Collections.sort(stringList, new ArrayComparator<String>(indexForSorting));