How sort array

18 views
Skip to first unread message

Rubén V

unread,
Dec 6, 2019, 11:41:55 PM12/6/19
to CodenameOne Discussions
Hi,

I have a trivial problem. I am trying to sort an array whose data model implements the PropertyBusinessObject classes. The field I want to sort by is "distance."

My code:

        Response<Map> jVisita = VisitaService.listaVisitas(System.currentTimeMillis());
        Map<Integer, Visita> m = jVisita.getResponseData();
        ArrayList tVisita = (ArrayList) Result.fromContent(m).getAsArray("root");
        ArrayList aVisita = new ArrayList();

        Visita v = new Visita();
        for (Object t : tVisita) {
            v.getPropertyIndex().populateFromMap((Map) t);
            v.distancia.set(distancia(v.latitud.get(), v.longitud.get()));
            aVisita.add(v);
        }

        aVisita.sort(Comparator.comparing(Visita::distancia.get()));  //SHOW ERROR

Dave Dyer

unread,
Dec 7, 2019, 2:42:01 PM12/7/19
to CodenameOne Discussions
Also make it implement java.lang.Comparable, use Arrays.sort


Shai Almog

unread,
Dec 7, 2019, 9:09:02 PM12/7/19
to CodenameOne Discussions
You asked how to sort an array but you're sorting an ArrayList which is a list.
You do that with Collections.sort().
Reply all
Reply to author
Forward
0 new messages