How to sort an Array ArrayList?

Hi!
I am working on a sorting algorithm but I am still having some trouble. I have an array list
ArrayList<ArrayList<Float>> segs = new ArrayList<ArrayList<Float>>();
and I want to sort() (width the default function) the ArrayList of Floats - segs.get(0).sort() (something like this but it doesn’t work)

How do I do it?



import java.util.Collections;  // Import the Collections class

ArrayList <Float> list = new ArrayList();

list. add (17.1); 
list. add (7.7);
list. add (12.9);

println(list); 

Collections.sort(list); 

println(list); 

1 Like

Hey!

Tested and working:

import java.util.Collections; //import Collections

ArrayList<ArrayList<Float>> segs = new ArrayList<ArrayList<Float>>();//your array

//Fill your array with random float values
for(int i=0; i<100; i++){
  segs.add(new ArrayList<Float>());
  
  for(int j=0; j<100; j++)
    segs.get(i).add(random(0,10));
}

printArray(segs.get(0)); //display the first row of seg

Collections.sort(segs.get(0)); //sort it by using Collections

printArray(segs.get(0)); //display the first row of seg sorted

You can achieve what you want by using the Collections class (you just give it your ArrayList and he do the job)

Alternatively you can use the faster FloatList container and its method sort():



import java.util.Collections;  // Import the Collections class

ArrayList <ArrayList <Float> > list = new ArrayList();

// --------------------------------------------------------------------------------

ArrayList <Float> listA = new ArrayList();

listA.add (17.1); 
listA.add (7.7);
listA.add (12.9);

list.add(listA); 

listA=null; 

// -----------------------------------------------
// Use 

println(list.get(0));  

Collections.sort(list.get(0)); 

println(list.get(0));

Oh yes! An ArrayList of FloatList might be nicer!

How is FloatList faster than ArrayList? (and how much?)

FloatList is backed by an internal float[] array while the corresponding ArrayList<Float> is a Float array instead:
https://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Float.html

The primitive float type is faster than an object Float, but nothing that big.

Here’s an example using that combo:

import java.util.List;

final List<FloatList> segs = new ArrayList<FloatList>();

for (int i = 0; i++ < 3; ) {
  final FloatList fl = new FloatList();
  segs.add(fl);
  for (int j = 0; j++ < 5; fl.append(random(-1000, 1000)));
}

for (final FloatList fl : segs)  println(fl);
println();

for (final FloatList fl : segs)  fl.sort();
for (final FloatList fl : segs)  println(fl);

exit();

And that same sketch but using a vanilla FloatList[] array instead:

final FloatList[] segs = new FloatList[3];

for (int i = 0; i < segs.length; ++i) {
  final FloatList fl = new FloatList();
  segs[i] = fl;
  for (int j = 0; j++ < 5; fl.append(random(-1000, 1000)));
}

for (final FloatList fl : segs)  println(fl);
println();

for (final FloatList fl : segs)  fl.sort();
for (final FloatList fl : segs)  println(fl);

exit();

ArrayList<ArrayList<Float>> segs = new ArrayList<ArrayList<Float>>();

In my opinion this is hard to read and to maintain.

Instead make a class MyFLoatClass and an Arraylist of that

Arraylist<MyFLoatClass> list = new ArrayList();

Inside the class MyFLoatClass have another ArrayList.

Much clearer.

Chrisir

to be honest I was trying to make the sort() as a placeholder until I code my own, but it looks like it is even more difficult : P

1 Like