# Sorting ArrayList

**URL:** <https://discourse.processing.org/t/sorting-arraylist/5083>\
**Category:** Coding Questions\
**Created:** [November 1, 2018, 12:20pm UTC](https://discourse.processing.org/t/sorting-arraylist/5083 "2018-11-01T12:20:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yann/32/2067_2.png) [@Yann](https://discourse.processing.org/u/Yann)\
**Post date:** [November 1, 2018, 12:20pm UTC](https://discourse.processing.org/t/sorting-arraylist/5083/1 "2018-11-01T12:20:08Z")

</div>

Hi,

I can’t find how to sort an ArrayList by one of the variables of the contained objects.

Let’s say I have a class like this one:

```auto
class Event {
  int position;
  int value;

  Event(int p, int v) {
    position = p;
    value = v;
  }
}

```

I want to sort my ArrayList of events by the events’ position or value. Isn’t there a way to do that other than creating the sorting algorithm myself?

Thanks in advance for your help.

---

<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:** [November 1, 2018, 12:25pm UTC](https://discourse.processing.org/t/sorting-arraylist/5083/2 "2018-11-01T12:25:24Z")

</div>

> [@Sort PVector array by distance](https://discourse.processing.org/t/sort-pvector-array-by-distance/4737/3):
>
> Use the sketch from this link below as a model: nerd_face[Forum.Processing.org/two/discussion/19469/sort-a-pvector-array#Item\_9](http://Forum.Processing.org/two/discussion/19469/sort-a-pvector-array#Item_9) But change its Comparator::compare() to use dist() instead. stuck_out_tongue

> [@Accessing functions from Java Generics](https://discourse.processing.org/t/accessing-functions-from-java-generics/3391/2):
>
> If you intend to sort() your arrays or collections by some numerical value, you can make your classes implements DoubleSupplier: import java.util.function.DoubleSupplier; class SomeClass implements DoubleSupplier { float x; @Override double getAsDouble() { return x; } } And use the Comparator below in order to sort() by ascending numerical order: import java.util.Comparator; import java.util.function.DoubleSupplier; import java.util.Arrays; import java.util.Collections; static f…

> [@Accessing functions from Java Generics](https://discourse.processing.org/t/accessing-functions-from-java-generics/3391/9):
>
> Even If we had 2 or more classes w/ the exactly same code, Java would still “see” them as different from each: void setup() { MyVec vec1 = new MyVec(); MyVecTwin vec2 = new MyVecTwin(); println(MyVec.class.isInstance(vec1)); // true println(MyVec.class.isInstance(vec2)); // false exit(); } class MyVec { float x = random(-100, 100), y = random(-100, 100), z; @Override String toString() { return "[" + x + ", " + y + ", " + z + "]"; } } class MyVecTwin { // exactly same…

---

<div class="post-metadata">

**Author:** ![Yann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yann/32/2067_2.png) [@Yann](https://discourse.processing.org/u/Yann)\
**Post date:** [November 1, 2018, 12:39pm UTC](https://discourse.processing.org/t/sorting-arraylist/5083/3 "2018-11-01T12:39:33Z")

</div>

Thank you. Unfortunately I don’t understand anything from those links ☹ I guess it’s less work to learn how to create a basic sorting algorithm than learning how to understand those code examples you posted… I just hoped there was an easy way. I guess it’s useful homework anyway.

---

<div class="post-metadata">

**Author:** ![Yann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yann/32/2067_2.png) [@Yann](https://discourse.processing.org/u/Yann)\
**Post date:** [November 1, 2018, 1:06pm UTC](https://discourse.processing.org/t/sorting-arraylist/5083/4 "2018-11-01T13:06:45Z")

</div>

I guess this is a nice place to start, in case anyone is having the same question…

[![](https://img.youtube.com/vi/JUDYkxU6J0o/hqdefault.jpg "Coding Challenge #47: Pixel Sorting in Processing") ](https://www.youtube.com/watch?v=JUDYkxU6J0o)

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [November 1, 2018, 2:41pm UTC](https://discourse.processing.org/t/sorting-arraylist/5083/5 "2018-11-01T14:41:32Z")

</div>

Hi Yann,

You can make your class implements the `Comparable` interface.

Then you need to override the `compareTo(Object o)` function. It return an `int` that is negative if the current object is smaller than the one you pass by argument, 0 if they are the same and bigger if positive.

Then you simply need a call to the `Collection.sort()` function.

Here is an example:

```auto
import java.util.Collections;

void setup() {
  ArrayList<Event> events = new ArrayList<Event>();

  for (int i = 0; i < 10; i++) {
    events.add(new Event((int)random(100)));
  }
  
  println("Before sorting:");
  for (Event e : events) {
    println(e.value);
  }

  Collections.sort(events);
  
  println("");
  println("After sorting:");
  for (Event e : events) {
    println(e.value);
  }
}

class Event implements Comparable<Event> {
  public int value;

  Event(int value) {
    this.value = value;
  }

  @Override
    int compareTo(Event other) {
      return this.value - other.value;
    }
}

```

---

<div class="post-metadata">

**Author:** ![Yann](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yann/32/2067_2.png) [@Yann](https://discourse.processing.org/u/Yann)\
**Post date:** [November 2, 2018, 10:13pm UTC](https://discourse.processing.org/t/sorting-arraylist/5083/6 "2018-11-02T22:13:25Z")

</div>

Thanks jb4x,

I could use the algo from the Coding Train tutorial. I tried your method as well, both worked, learnt 2 ways of sorting an array, happy 😃 Java is a lot more complex than Processing suggests, it’s kind of scary but good to know in the same time!
