1

I am trying to sort List<Map<String, Object>> from a request to an oracle database. The format I am receiving is this

63: "Preasignación"
401: "Categorización de posiciones RPV"
509: "Genérica"
532: "Baja de conservación de número"
537: "Pooles ADSL RIMA"
660: "Activación"
886: "CENTREX"
905: "Conservación de número"
920: "Suspensión y rehabilitación"
955: "STB, AABB, PBX e ISPBX"

As you can see they are sorted by numbers, but I want to sort them by value "alphabetically". How can I do it? Please try to not freak out with spanish code :) this is inherited code. Here my code.

public ArrayList<String> ObtenerIdsTiposOrdenPorPerfilLdap(String perfiles) {

    String query = "";
    ArrayList<String> tipos = new ArrayList<String>();
    try {
        query = " SELECT ID_PARAMETRO FROM PARAMETRO WHERE EXISTS( " +
                    " SELECT DISTINCT(ID_TIPO_ORDEN) FROM REL_PERF_LDAP_PERF_TP_ORD_SIS " +
                    " WHERE ID_PERFIL_LDAP IN (" + perfiles + ") " +
                    " AND ID_TIPO_ORDEN = -1) " +
                    " AND ID_TIPO_PARAMETRO = " + Parametro.ID_TIPO_PARAMETRO_TIPO_ORDEN +
                    " UNION " +
                    " SELECT ID_PARAMETRO FROM PARAMETRO WHERE ID_PARAMETRO IN ( " +
                    " SELECT DISTINCT(ID_TIPO_ORDEN) FROM REL_PERF_LDAP_PERF_TP_ORD_SIS " +
                    " WHERE ID_PERFIL_LDAP IN (" + perfiles + ")) " +
                    " AND ID_TIPO_PARAMETRO = " + Parametro.ID_TIPO_PARAMETRO_TIPO_ORDEN +
                    " ORDER BY ID_PARAMETRO ";
        List<Map<String, Object>> result = jdbcTemplate.queryForList(query);
        for (Map<String, Object> map : result) {
            tipos.add(String.valueOf(map.get("ID_PARAMETRO")));  
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    } finally {
        query = null;
    }
    return tipos;
}
Elarbi Mohamed Aymen
  • 1,617
  • 2
  • 14
  • 26
Tony
  • 37
  • 5
  • 5
    Possible duplicate of [Sort a Map by values](https://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values) – Bojan Trajkovski Oct 18 '18 at 11:41
  • Use [`TreeMap`](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html) – KaiserKatze Oct 18 '18 at 11:43
  • @KaiserKatze I have tried with TreeMap, but as you can see it is ordered by number, and I want alphabetically. Thanks for spare some of your time – Tony Oct 18 '18 at 11:45
  • Using a `Map` to store the results is bad design anyway. Create a class with an `int` for `id` and `String` for `parameterName` and create those with the query. You can sort `List`s containing them by creating different `Comparator`s then. – daniu Oct 18 '18 at 11:46
  • 1
    The data your are receiving looks like more of `Map` than List>, – Matthieu Gabin Oct 18 '18 at 11:55
  • 1
    Your SQL does not retrieve the text values ("preasignación", ...), so I think there is some code missing in the question. – gpeche Oct 18 '18 at 12:16
  • @Tony Did you try [`public TreeMap(Comparator super K> comparator)`](https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html#TreeMap-java.util.Comparator-)? – KaiserKatze Oct 19 '18 at 01:40

1 Answers1

1

If the textual descriptions of PARAMETRO ("preasignación", ...) are actually in PARAMETRO itself (let's call that column TXT_PARAMETRO), it should be enough to ask the database to do the ordering: remove

" ORDER BY ID_PARAMETRO ";

and add

" ORDER BY TXT_PARAMETRO ";

instead.

gpeche
  • 21,974
  • 5
  • 38
  • 51
  • I see that you have accepted my answer: be aware that now *every* piece of code that invokes `ObtenerIdsTiposOrdenPorPerfilLdap` will get the results _in a different order than before_ ! This might have unintended consequences. Consider either parameterising the query ordering or having two methods returning the results in a different order, so you can implement your modifications without affecting existent code. – gpeche Oct 18 '18 at 12:57