简体   繁体   English

在Java中对多个arraylists进行排序

[英]Sorting multiple arraylists in Java

I'm trying to figure out how to sort the arrayLists that come out of a .txt file. 我试图弄清楚如何对来自.txt文件的arrayLists进行排序。 I want to be able to sort them alphabetically by name. 我希望能够按名称按字母顺序对它们进行排序。 Here is an example of how the txt file is listed: 以下是如何列出txt文件的示例:

Alvarez, Eliezer
74
2B
IA
22
Bowman, Matt
67
P
A
26

Each piece is on a single line by itself (except lastName, firstName being on a line together). 每个部分本身都在一行上(除了lastName,firstName一起在一行上)。

Is there a way to do a collections sort that will adjust the rest of the Arraylists based off the name Arraylist? 有没有办法进行集合排序,根据名称Arraylist调整其余的Arraylists? Thanks. 谢谢。

    Scanner keyboard = new Scanner(System.in);
    String filename;
    fileName = "cardinals.txt";

    File baseball = new File(fileName);
        if (!baseball.exists()) {
            System.out.println("The input file was not found.");
            System.exit(0);
        }

    ArrayList<String> name = new ArrayList<>();
    ArrayList<Integer> number = new ArrayList<>();
    ArrayList<String> position = new ArrayList<>();
    ArrayList<String> status = new ArrayList<>();
    ArrayList<Double> age = new ArrayList<>();

    Scanner stats = new Scanner(baseball);

    if (!stats.hasNext()) {
        System.out.println("The file is empty.");
    }
    else {
        while (stats.hasNext()) {
            name.add(stats.nextLine());
            number.add(stats.nextInt());
            position.add(stats.next());
            status.add(stats.next());
            age.add(stats.nextDouble());
            try {
                stats.nextLine();
            } catch (Exception e) {
            }
        }
    } stats.close();
    sortArrayPosition();

    sortArrayPosition (ArrayList<String> name, ArrayList<Integer> number, ArrayList<String> position, ArrayList<String> status, ArrayList<Double> age) {

When organizing and managing complex data, using an object to group the data together is the best approach. 组织和管理复杂数据时,使用对象将数据分组在一起是最好的方法。

Here is an example using an object to store the information: 以下是使用对象存储信息的示例:

package example;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

class StatisticsReader {
    private static class PlayerStats {
        String name;
        Integer number;
        String position;
        String status;
        Double age;

        public PlayerStats(String name, Integer number, String position, String status, Double age) {
            this.name = name;
            this.number = number;
            this.position = position;
            this.status = status;
            this.age = age;
        }

        @Override
        public String toString() {
            return "PlayerStats{" +
                    "name='" + name + '\'' +
                    ", number=" + number +
                    ", position='" + position + '\'' +
                    ", status='" + status + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    public static void main(String[] args) {
        File baseball = new File("cardinals.txt");
        if (!baseball.exists()) {
            System.out.println("The input file was not found.");
            System.exit(0);
        }
        List<PlayerStats> statsList = new ArrayList<>();
        try (Scanner stats = new Scanner(baseball)) {
            if (!stats.hasNext()) {
                System.out.println("The file is empty.");
            }
            else {
                while (stats.hasNext()) {
                    String name = stats.nextLine();
                    Integer number = stats.nextInt();
                    String position = stats.next();
                    String status = stats.next();
                    Double age = stats.nextDouble();
                    statsList.add(new PlayerStats(name, number, position, status, age));
                    stats.nextLine();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        statsList.sort(Comparator.comparing(o -> o.name));
        statsList.forEach(System.out::println);
    }
}

You can customize the toString() method to change the output if necessary. 您可以自定义toString()方法以在必要时更改输出。

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

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