2017-02-09 4 views
0

電流出力ファイル。のJavaソートは

どのようにすれば、最初に最も高いマークでソートし、次に最も低い試行にするようにしますか?例xxxxxは、6,1、xxxxは、6,1 xxxxは、6,4、.....

ヘルプ!!!!!!何時間も立ち往生!

public static void sort() { 
    BufferedReader reader = null; 
    try { 
     reader = new BufferedReader(new FileReader("data.csv")); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    Map < String, List <String>> map = new TreeMap < String, List <String>>(); 
    String line; 
    try { 
     line = reader.readLine(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } //read header 
    try { 
     while ((line = reader.readLine()) != null) { 
      String key = getField(line); 

      List <String> l = map.get(key); 

      if (l == null) { 
       l = new LinkedList <String>(); 
       map.put(key, l); 
      } 
      l.add(line); 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     reader.close(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    System.out.println("HALL OF FAME\nMatric, Marks, Attempt"); 
    map = ((TreeMap < String, List <String>>) map).descendingMap(); 

    for (List <String> list: map.values()) { 
     for (String val: list) { 
      System.out.println(val); 
     } 
    } 
} 


private static String getField(String line) { 
    return line.split(",")[1]; // extract value you want to sort on 
} 
+2

なぜスクリーンショット?あなたの出力を質問に貼り付けるだけです。 – shmosel

+0

あなたの投稿に含めるべきデータへのリンクを投稿しないでください。あなたの提供されたリンクが死んでしまうと、あなたの質問は他の人を助けることをどのように期待していますか? – DevilsHnd

答えて

0

動作しているようです!

public static void sort() throws FileNotFoundException { 
    BufferedReader reader; 
    try { 
     reader = new BufferedReader(new FileReader("data.csv")); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     throw e; 
    } 
    Map<MarkAndAttempt, List<String>> map = 
      new TreeMap<MarkAndAttempt, List<String>>(new Comparator<MarkAndAttempt>() { 
       @Override 
       public int compare(MarkAndAttempt o1, MarkAndAttempt o2) { 
        if (o1.getMark() > o2.getMark()) { 
         return -1; 
        } else if (o1.getMark() == o2.getMark()) { 
         return o1.getAttempt() > o2.getAttempt() ? 1 : -1; 
        } else { 
         return 1; 
        } 
       } 
      }); 
    String line; 
    try { 
     reader.readLine(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    }//read header 
    try { 
     while ((line = reader.readLine()) != null) { 
      //String key = getField(line); 

      //List<String> l = map.get(key); 

      MarkAndAttempt markAndAttempt = generateMarkAndAttempt(line); 
      List<String> l = map.get(markAndAttempt); 

      if (l == null) { 
       l = new LinkedList<String>(); 
       map.put(markAndAttempt, l); 
      } 
      l.add(line); 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     reader.close(); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 

    System.out.println("HALL OF FAME\nMatric, Marks, Attempt"); 

    for (List<String> list : map.values()) { 
     for (String val : list) { 
      System.out.println(val); 
     } 



    } 
} 


private static MarkAndAttempt generateMarkAndAttempt(String line) { 
    Objects.requireNonNull(line); 
    String[] parts = line.split(","); 
    if (parts.length != 3) throw new IllegalArgumentException(""); 
    return new MarkAndAttempt(Integer.valueOf(parts[1].trim()), Integer.valueOf(parts[2].trim())); 
} 

private static String getField(String line) { 
    return line.split(",")[1];// extract value you want to sort on 
} 

static class MarkAndAttempt { 

    private int mark; 

    private int attempt; 

    public MarkAndAttempt(int mark, int attempt) { 
     this.mark = mark; 
     this.attempt = attempt; 
    } 

    public int getMark() { 
     return mark; 
    } 

    public void setMark(int mark) { 
     this.mark = mark; 
    } 

    public int getAttempt() { 
     return attempt; 
    } 

    public void setAttempt(int attempt) { 
     this.attempt = attempt; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     MarkAndAttempt that = (MarkAndAttempt) o; 

     return mark == that.mark; 

    } 

    @Override 
    public int hashCode() { 
     return mark; 
    } 
} 
+0

この行に複数のマーカー。コンパレータを型に解決することはできません。コンストラクタツリーマップ....は未定義です。 – Tosm

+0

メソッドcompare(main ...)は、スーパーメソッドをオーバーライドまたは実装する必要があります。googling – Tosm

+0

ソースcsvファイルを表示できます –