B4J Question B4J and Java Comparator

Johan Schoeman

Expert
Licensed User
Longtime User
I have a B4J list and each item in the list consists of an array(5) of double. I now need to sort the list in descending order using the 1st item of the array i.e on item at index 0 in the arrays of doubles within the list. Below is how they do it in the Java project that I am trying to convert to B4J. It uses Collections and a Comparator. How would one do this in B4J? Would it be better to convert the array of doubles to a Type and then sort the list according to the name of the fist type in the Type that one creates?

B4X:
    public ArrayList<double[]> sortArraylistOfDoubleArray(ArrayList<double[]> sortinglist) {
        Collections.sort(sortinglist, new Comparator<double[]>() {
            //Sorting cycles by amplitude in descending order
            @Override
            public int compare(double[] o1, double[] o2) {
                int first = (int) o1[0];
                int second = (int) o2[0];
                BA.Log("in compare");
                return first > second ? -1 : (first < second) ? 1 : 0;
            }
        });
        BA.Log("out of compare");
        return sortinglist;
    }

Example of what the list looks like before sorting it on index 0 of the arrays in each list entry
List(0) = array of double (2.9, 3.1, 4.3, 5.9, 2.5)
List(1) = array of double(7.4, 1.5, 5.2, 9.2, 7,3)
List(2) = array (of double(4.9, 3.6, 2.2, 4.2, 5.9)

After the sort it should look like this:
List(0) = array of double(7.4, 1.5, 5.2, 9.2, 7.3)
List(1) = array (of double(4.9, 3.6, 2.2, 4,2, 5.9)
List(2) = array of double (2.9, 3.1, 4.3, 5.9, 2.5)
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
May be you can set your own algorithm:
 
Upvote 1

Johan Schoeman

Expert
Licensed User
Longtime User
May be you can set your own algorithm:
I have seen it - busy trying to implement it in my B4J project.
 
Upvote 0
Top