首页 > 解决方案 > 如何对文件中的文本行块进行排序?

问题描述

我有一个文本文件,其中包含 ID、人名和姓氏、银行余额和帐户创建日期。我不知何故需要按姓氏和平衡对这个文本文件进行排序,但我不知道如何。

ID:41
Name and surname: Andris Lietis
Balance: 30000 Eur
Created: 31.03.2019

ID:33
Name and surname: Rainis Maize
Balance: 2000 Eur
Created:  32.03.2019

ID:34
Name and surname: Akmens Liepins
Balance: 123 Eur
Created:  29.03.2019

ID:35
Name and surname: Karlis Maiznieks
Balance: 32000 Eur
Created:  04.04.2019

所以我首先考虑以某种方式读取该文件,当我接近 ID 行之一时,我读取了接下来的三行并以某种方式将这 4 行存储在某种块/变量中,然后对其他块执行相同的操作。我的朋友提到制作对象和比较,但我对此没有任何经验,当我在谷歌中查找时,这真的让我感到困惑。

    public static void Sakartosana(String nosaukums) throws IOException {


        br = new BufferedReader(new FileReader(nosaukums));
        String s;
//I understand that I need to somehow sort this with selection.sort
        ArrayList<String> al = new ArrayList<String>(); 
        while ((s = br.readLine()) != null) {
            if (s.startsWith("ID:")) {

            }

        }
        br.close();

    }

这就是我逐行阅读的方式,我有很多不同任务的方法来读取这样的文件。

预期的结果是这样的:

ID:41
Name and surname: Andris Lietis
Balance: 30000 Eur
Created: 31.03.2019

ID:34
Name and surname: Akmens Liepins
Balance: 123 Eur
Created:  29.03.2019

ID:35
Name and surname: Karlis Maiznieks
Balance: 32000 Eur
Created:  04.04.2019

ID:33
Name and surname: Rainis Maize
Balance: 2000 Eur
Created:  32.03.2019

标签: javafilesorting

解决方案


推荐阅读