# How to sort an Array ArrayList?

**URL:** <https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809>\
**Category:** Beginners\
**Created:** [January 5, 2021, 10:11pm UTC](https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809 "2021-01-05T22:11:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 5, 2021, 10:11pm UTC](https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809/1 "2021-01-05T22:11:22Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2021, 11:17pm UTC](https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809/2 "2021-01-05T23:17:29Z")

</div>

```auto

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); 

```

---

<div class="post-metadata">

**Author:** ![YumKlarc](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yumklarc/32/11689_2.png) [@YumKlarc](https://discourse.processing.org/u/YumKlarc)\
**Post date:** [January 5, 2021, 11:18pm UTC](https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809/3 "2021-01-05T23:18:20Z")

</div>

Hey!

Tested and working:

```auto
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)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 5, 2021, 11:19pm UTC](https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809/4 "2021-01-05T23:19:28Z")

</div>

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

> **[FloatList::sort() \\ Language (API) \\ Processing 3+](https://Processing.org/reference/FloatList_sort_.html)**

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2021, 11:20pm UTC](https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809/5 "2021-01-05T23:20:11Z")

</div>

```auto

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));

```

---

<div class="post-metadata">

**Author:** ![YumKlarc](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yumklarc/32/11689_2.png) [@YumKlarc](https://discourse.processing.org/u/YumKlarc)\
**Post date:** [January 5, 2021, 11:23pm UTC](https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809/6 "2021-01-05T23:23:58Z")

</div>

Oh yes! An ArrayList of FloatList might be nicer!

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 5, 2021, 11:44pm UTC](https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809/7 "2021-01-05T23:44:42Z")

</div>

> [@YumKlarc](#):
>
> How is FloatList faster than ArrayList?

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](https://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Float.html)

> [@YumKlarc](#):
>
> (and how much?)

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

> [@YumKlarc](#):
>
> Oh yes! An ArrayList of FloatList might be nicer!

Here’s an example using that combo:

```auto
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:

```auto
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();

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 5, 2021, 11:56pm UTC](https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809/8 "2021-01-05T23:56:37Z")

</div>

`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

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 6, 2021, 6:45am UTC](https://discourse.processing.org/t/how-to-sort-an-array-arraylist/26809/9 "2021-01-06T06:45:32Z")

</div>

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
