简体   繁体   English

Java,对txt文件中的数据进行排序和显示

[英]Java, sort and display data from txt file

i'm new with java and have some trouble with one task. 我是Java的新手,在完成一项任务时遇到了一些麻烦。 i have txt file which looks like this: 我有看起来像这样的txt文件:

John Doe,01-01-1980,Development,Senior Developer
Susan Smith,07-12-1983,Development,Head of Development
Ane Key,06-06-1989,BA,Junior Analyst
Nina Simone,21-09-1979,BA,Head of BA
Tom Popa,23-02-1982,Development,Developer
Tyrion Lannyster,17-03-1988,BA,Analyst

and i want to to sort it by departments. 我想按部门分类。 for example: Members are : 例如:成员是:

[Employee Full Name] - [Employee Age] - [Employee Position] - [Employee Salary(default value x)]

Deparment : Development Members are : Susan Smith ...... John Doe ...... Tom Popa ...... Department : BA Members are : Nina Simone ....... Ane Key ........... Tyrion Lannyster ........ 部门:发展部成员:Susan Smith ...... John Doe……Tom Popa……部门:BA成员:Nina Simone ....... Ane Key ... ........ Tyrion Lannyster ........

at first read file and made 2d array but can't continue how to correctly sort it. 最初读取文件并制作2D数组,但无法继续如何正确对其进行排序。

public static void main(String[] args) {

    String csvFile = "C:\\Employees.txt";
    BufferedReader br = null;
    String line = "";
    String SplitBy = ",";

    String myArray[][] = new String[6][5];
    int row = 0;

    try {
        br = new BufferedReader(new FileReader(csvFile));

        while ((line = br.readLine()) != null) {  

            String nums[] = line.split(SplitBy);

        for (int col = 0; col < nums.length; col++){

                String n =nums[col];

                myArray[row][col] = n;
           //     System.out.println(n);
            }               
            row++;



        }

              } 

    catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



}

The Java Collections API allows you to Sort as well as util.Arrays . Java Collections API允许您以及util.Arrays进行排序。 You will need the arrays methods for you code, but consider moving to some sort of Collection. 您将需要用于代码的arrays方法,但请考虑移至某种Collection。 Perhaps a List to start with. 也许从列表开始。

The easiest way would put the contents of the lines in Java Beans and then sort them using sort. 最简单的方法是将行的内容放入Java Bean中,然后使用sort对其进行排序。

public User { 
    private String name;
    // ... all the fields with getters and setters
}

Then adapt your code to something like this: 然后将您的代码修改为如下所示:

// create a nice List for the users.
List<User> userList = new ArrayList<>();

while ((line = br.readLine()) != null) {
            User user = new User();
            String nums[] = line.split(SplitBy);
            user.setName(nums[0]);
            // create nice method to convert String to Date
            user.setDate(convertStringToDate(nums[1]));
            // add the user to the list
            userList.add(user);

}

// Then finally sort the data according to the desired field.
Arrays.sort(userList, (a,b) -> a.name.compareTo(b.name));

Providing the code solution here would not help you learn it. 在此处提供代码解决方案不会帮助您学习。 But I can give you hints on how to proceed. 但我可以给您提示如何进行。 Using array is not really recommended. 确实不建议使用数组。

The easy, but dirty way - 简单但肮脏的方式-

Instead of two dimensional array, use a TreeMap<String, String[]> where key is the department concatenated with name and value is the one dimensional array of the individual's details. 代替二维数组,请使用TreeMap<String, String[]> ,其中key是与名称连接的部门,value是个人详细信息的一维数组。 As we're using TreeMap , the result is naturally sorted based on Department followed by Name of the person. 当我们使用TreeMap ,结果自然地根据Department后面跟人的名字进行排序。 Loop through the entrySet and print all the results. 遍历entrySet并打印所有结果。

Right way - 正确的路 -

Define new object Person with all the members needed. 用所需的所有成员定义新对象Person Implement Comparable interface. 实现Comparable接口。 Read all the input data, populate the same in Person object, add each such objects in an ArrayList and use Collections API's sort method to sort all the objects. 读取所有输入数据,在Person对象中填充相同的数据,在ArrayList添加每个此类对象,然后使用Collections API的sort方法对所有对象进行排序。 Alternatively you can adapt the Comparator way. 另外,您可以采用Comparator方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM