sort arraylist using startwith for api < 24

Viewed 453

I created an autocompleteTextView that shows the results that I got from a client depanding of the query the user entered.

The results from the client arent ordered in any way.

For example, if the users typed harry pot I could get:

harry and the tree
the trees and harry
harry pot right
ben and harry
harry and be

and so on.

I would like that the results will appear by relevance = if the user typed in harry pot I want to assume that it starts with harry pot.

harry pot right
harry and be
harry and the tree
the trees and harry
ben and harry

How could I sort my ArrayList to show first the results start with this query?

I tried to use stream or comperator but both require API >= 24.

Any way to do it for lower API? (specifically needs API = 21).

Thank you

2 Answers

Try

Collections.sort(arrayList, new Comparator<Element>() {
    @Override
    public int compare(Element element, Element t1) {
        if(element.time > t1.time) return -1;
        else return 1;
    }
});

It work for API 23

Related