Ordering a Sortedmap by object attribute

Asked

Viewed 98 times

0

Good afternoon fellow programmers on duty. I am developing an application that creates a formulery through the information contained in a tabulato.txt file. I managed to develop the whole routine of the creation of the formúlario, now I need to order it, according to the attribute "geopolitical" (neighborhoods). Follow the code below:

MAP CREATION CLASS:

    package classes.repository;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.SortedMap;
    import java.util.TreeMap;
    import classes.model.Clientes;
    import classes.model.Comodatos;
    import classes.model.Zonas;

            public class Tables {
        public SortedMap<String, Clientes> clientes = new TreeMap<>();
public List<Zonas> zonas = new ArrayList<>();
public Map<String, Comodatos> comodatos = new LinkedHashMap<>();

public void tables() {
    try {
        File file = new File("table.txt");
        FileReader fReader = new FileReader(file);
        BufferedReader bReader = new BufferedReader(fReader);
        String leitor = bReader.readLine();
        while ((leitor = bReader.readLine()) != null) {
            String[] leitorSplit = leitor.split("\t");
            clientes.put(leitorSplit[0], new Clientes(leitorSplit)); // leitorSplit[0] == cód do cliente não pode se repetir, por isso utilizei o Map
            comodatos.put(leitorSplit[5], (new Comodatos(leitorSplit)));
        }
        File file = new File("zonasbairros.txt");
        FileReader fReader = new FileReader(file);
        BufferedReader bReader = new BufferedReader(fReader);
        String leitor;
        while ((leitor = bReader.readLine()) != null) {
            String[] leitorSplit = leitor.split("\t");
            zonas.add(new Zonas(leitorSplit));
        }
        bReader.close();
    } catch (IOException e) {
        e.getMessage();
    }
}

}

I implemented a comparable in the object class to see if it worked, but it didn’t work:

    package classes.model;
    public class Clientes implements Comparable<Clientes> {

private String cod, fantasia, razaoSocial, endereco, geopolitico, documento, codproduto, descricao, qtd;
private String[] linha;

public Clientes() {
}

public Clientes(String[] linha) {
    this.cod = linha[0];
    this.fantasia = linha[1];
    this.razaoSocial = linha[2];
    this.endereco = linha[3];
    this.geopolitico = linha[4];
}


public String getCod() {
    return cod;
}

public void setCod(String cod) {
    this.cod = cod;
}

// Getters...Setters..

@Override
public int compareTo(Clientes o) {
    return geopolitico.compareTo(o.geopolitico);
}

}

You could easily organize with Collections.Sort if you use a List, however the data of the.txt file is repeated and so the choice of Map. Several heads think better than one. Any light?? hug!

  • The TreeMap Java is based on a RedBlackTree and the key is used to compare tree nodes, which means it is automatically ordered by keys, and "impossible" to sort by values. An alternative is to rebuild the tree by reimplementing the comparison function used by the tree for insertion, so that it compares by value rather than by key. Or use another data structure that has no restriction.

  • Positive Isac. Thanks for the help,. I solved the problem by instantiating a List<Customers> with the data from Sortedmap<Customers>. In this way I managed to arrange the information without problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.