2016-11-08 38 views
0

目前,我只是在文件中對名稱進行排序的位置,但我也希望這樣做,以便年齡可以排序。另一個問題是嘗試獲取相同但具有不同年齡的姓名進行排序。現在我的代碼看起來是這樣的:如何同時對兩個數字和名稱進行排序

import java.io.BufferedReader; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.lang.reflect.Array; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 


public class MultiKey { 

    public static void main(String[] args) { 
     File textFile = new File("H:\\Names_ages.txt"); 
     FileReader in; 
     BufferedReader readFile; 
     String lineOfText; 

     try { 
      in = new FileReader(textFile); 
      readFile = new BufferedReader(in); 
      BufferedReader reader = new BufferedReader(new FileReader(textFile)); 
      List<String> results = new ArrayList<String>(); 
      while ((lineOfText = readFile.readLine()) != null) { 
       results.add(lineOfText); 

      } 
      Collections.sort(results); 
      System.out.println(results); 


      readFile.close(); 
      in.close(); 
     } catch (FileNotFoundException e){ 
      System.out.println("File does not exist or could not be found"); 
      System.err.println("FileNotFoundException: "+ e.getMessage()); 
     } catch (IOException e){ 
      System.out.println("Problem reading file"); 
      System.err.println("IOException: " + e.getMessage()); 
     } 
    } 
} 
+1

如果要排序以多種方式您的收藏,您可以通過編寫比較爲此,http://stackoverflow.com/questions/5245093/using-comparator-to- make-custom-sort比較器允許您根據指定的方法對集合進行排序。 – chatton

+0

只是一張紙條,我現在得到的輸出是:[Abrams 15,Alexander 22,Herkman 12,Jones 11,Jones 14,Jones 2,Jones 9,Smith 17,Smith 19,Smith 20,Tippurt 42] –

+0

應該查看['try-with-resources'](https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)。 – bradimus

回答

0

典型的做法是:

  • 分析每一行,併爲其 有兩個創建一個封裝對象(在你的例子一類「人」字段爲「名稱」和「年齡」) 你如何做這個解析取決於文件中的行的格式 eg如果行 中的值由逗號分隔,則可以使用String.split(「,」)。

  • 將封裝對象添加到列表中,然後例如使用 比較器進行排序。使用java.util.Collections.sort(比較器)。

當然,使用該封裝對象列表,您可以非常輕鬆地完成更多操作,例如,找到名字相同但年齡不同的人。

1

邏輯:

  • 創建你想擁有排序上的屬性單獨的支持。
  • 在該Person對象上應用Comparator。

    import java.io.BufferedReader; 
    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.FileReader; 
    import java.io.IOException; 
    import java.lang.reflect.Array; 
    import java.util.ArrayList; 
    import java.util.Arrays; 
    import java.util.Collections; 
    import java.util.List; 
    
    
    public class MultiKey { 
    
        public static void main(String[] args) { 
         File textFile = new File("H:\\Names_ages.txt"); 
         FileReader in; 
         BufferedReader readFile; 
         String lineOfText; 
    
         try { 
          in = new FileReader(textFile); 
          readFile = new BufferedReader(in); 
          BufferedReader reader = new BufferedReader(new FileReader(textFile)); 
          List<Person> results = new ArrayList<Person>(); 
          while ((lineOfText = readFile.readLine()) != null) { 
           //split here the line into name and age separate variables basedon delimiter available between them. 
           Person p = new Person(name,age); 
           results.add(p); 
    
          } 
          order(results); 
          System.out.println(results); 
    
    
          readFile.close(); 
          in.close(); 
         } catch (FileNotFoundException e){ 
          System.out.println("File does not exist or could not be found"); 
          System.err.println("FileNotFoundException: "+ e.getMessage()); 
         } catch (IOException e){ 
          System.out.println("Problem reading file"); 
          System.err.println("IOException: " + e.getMessage()); 
         } 
        } 
    } 
    
    private static void order(List<Person> persons) { 
    
        Collections.sort(persons, new Comparator<Person>() { 
    
         public int compare(Object o1, Object o2) { 
    
          String x1 = ((Person) o1).getName(); 
          String x2 = ((Person) o2).getName(); 
          int sComp = x1.compareTo(x2); 
    
          if (sComp != 0) { 
           return sComp; 
          } else { 
           Integer x1 = ((Person) o1).getAge(); 
           Integer x2 = ((Person) o2).getAge(); 
           return x1.compareTo(x2); 
          } 
        }}); 
    } 
    
    public class Person{ 
    private String name; 
    private int age; 
    
    public String getName(){ 
    return this.name; 
    } 
    
    public void setName(String name){ 
    this.name = name; 
    } 
    
    public int getAge(){ 
    return this.age; 
    } 
    
    public vois setAge(int age){ 
    this.age = age; 
    } 
    

    }

0

可以使用thenComparing到鏈Comparator小號

Comparator<String> byName = Comparator.comparing(s -> s.split(" ")[0]); 
    Comparator<String> byAge = Comparator.comparingInt(s -> Integer.parseInt(s.split(" ")[1])); 

    try (BufferedReader br = new BufferedReader(new FileReader("filePath"))) { 
     List<String> sorted = br.lines().sorted(byName.thenComparing(byAge)).collect(Collectors.toList()); 
     return sorted; 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

如果不止一個空間也有望試模式\\s+而不是白色空間

或我們可以創建一個像b一樣的Comparator elow,而不是創建兩個Comparator小號

Comparator<String> c = Comparator.<String, String> comparing(s -> s.split("\\s+")[0]) 
      .thenComparingInt(s -> Integer.parseInt(s.split("\\s+")[1])); 
相關問題