How to retrieve certain string in text file and sort based on element in string?

jayjay275 :

I'm attempting to display a list of elements from a txtfile, and then sort the list according to the date of each record. I've used a hashset to load the txtfile completely and that works.

The main problem is that I can't seem to find a method to sort the txtfile and each record according to the dates contained under each record.

btnPrintList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {

// File path & name

Path path = Paths.get(System.getProperty("user.dir")).resolve("vehicle.txt");

try {
   BufferedReader reader = new BufferedReader(new 
   FileReader(path.toFile()));

   Set<String> carsPrint = new HashSet<>();

    String line = reader.readLine();
      while (line != null) {                 
            System.out.println("processing line: " + line);

        if (line.trim().contains("-")) {
          String[] words = line.split("");
                    }

        if (!line.trim().equals("")) {
          String[] words = line.split("");

                    }

        line = reader.readLine();

            }

        } 
              catch (IOException e1) {
        block
           e1.printStackTrace();
                }
            }

        });

Here is the textfile being referred to

a, Honda Jazz, Hatchback, grey, 20000, none, manual, 8000, 2018-09-01, 2018-10-04,
b, Mercedes Citan, Van, small, white, 35000, front damage, manual, 9500, 2018-11-04,
c, Ford Transit, Van, small, black, 60000, none, automatic, 7300, 2018-11-01,
d, Lexus IS, Saloon, white, 23000, indent on passenger door, automatic, 18100, 2019-01-02,
e, Lexus IS, Saloon, dark blue, 6000, none, manual, 26100, 2018-10-04, 2018-10-14,
f, Mercedes Citan, Van, small, white, 35900, front windscreen damage, automatic, 10000, 2018-08-04,
g, Lexus NX, SUV, dark blue, 12000, none, automatic, 31000,
h, Ford SMax, MPV, red, 70000, none, manual, 11000, 2017-01-13,
i, Vauxhall Movano, Van, large, white, 60000, rear windscreen damage, manual, 5000, 2019-01-10, 2019-03-01,
j, Vauxhall Insignia, Saloon, green, 45000, none, automatic, 10800, 2018-08-10, 2019-03-05,
k, Vauxhall Insignia, Saloon, red, 40000, none, manual, 10200, 2018-08-10,
l, Vauxhall Movano, Van, large, silver, 60000, none, automatic, 8800,
m, BMW 3, Saloon, black, 49000, door damage, manual, 9500, 2019-03-01, 2019-03-05,
n, Ford SMax, MPV, red, 70000, none, automatic, 3800,
o, Mercedes EClass, Coupe, yellow, 25000, none, automatic, 10400, 2018-03-08, 2019-03-02,
p, Jaguar FType, Coupe, silver, 3000, none, automatic, 52000, 2018-03-01,
pobu :

By definition, the hashset structure cannot be sorted. It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. (from Documentation)

But you can use ArrayList instead:

List<?> sortedCarsPrint  = new ArrayList<>(carsPrint);
sortedCarsPrint.sort((o1,o2) -> o1.getDateTime().compareTo(o2.getDateTime()));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157704&siteId=1