# How to sort coordinates?

**URL:** <https://discourse.processing.org/t/how-to-sort-coordinates/45599>\
**Category:** Coding Questions\
**Created:** [January 15, 2025, 8:30am UTC](https://discourse.processing.org/t/how-to-sort-coordinates/45599 "2025-01-15T08:30:40Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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 16, 2025, 2:14am UTC](https://discourse.processing.org/t/how-to-sort-coordinates/45599/4 "2025-01-16T02:14:44Z")

</div>

Proper ways to do sorting in Java is by instantiating a Comparator w/ a **compare()** callback method:

> [@Sorting an array](https://discourse.processing.org/t/sorting-an-array/20992/4):
>
> [Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Comparator.html#compare(T,T)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Comparator.html#compare(T,T))[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Comparator.html#reversed()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Comparator.html#reversed())[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#sort(T[],java.util.Comparator)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Arrays.html#sort(T%5B%5D,java.util.Comparator)) // https://Discourse.processing.org/t/sorting-an-array/20992/4 // GoToLoop (2020/May/18) import java.util.Arrays; import java.util.Comparator; final PVector[] vecs = { new PVector(100, -100), new PVector…

Or defining a Comparable class w/ a **compareTo()** callback method:

> [@The operator \> is undefined for the argument - HOW TO RESOLVE ERROR (OOP)](https://discourse.processing.org/t/the-operator-is-undefined-for-the-argument-how-to-resolve-error-oop/38705/6):
>
> If you wish you can take a look at a very old deck of cards implementation I did a long time ago: [Picking cards at random, then excluding those from further picking. - Processing 2.x and 3.x Forum](https://forum.Processing.org/two/discussion/2801/picking-cards-at-random-then-excluding-those-from-further-picking-#Item_2) It’s split into 2 sketches: 1 creates & saves the “.csv” file, while the other loads it and makes a deck of cards out of it. For convenience I’ve merged them now into 1 sketch w/ 4 “.pde” tabs: “Table\_CSV\_Card\_Pack.pde”: /\*\* \* Table CSV Card Pack (v1.0.1) \* by GoToLoop (2022/Sep/13) \* \* htt…

> [@What does "Error on int" mean?](https://discourse.processing.org/t/what-does-error-on-int-mean/42264/13):
>
> Now I understand why you wanted findClosestPaletteColor() to be a regular instance method: You’ve got rid of its 1st parameter! Instead the previous parameter became a clone of the current C3 instance. Just so you know, you can keep both static & non-static versions for findClosestPaletteColor(). And those new r(), g() & b() helper functions can be converted to static inside C3Base: static abstract class C3Base { static final color r(final color c) { return c \>\> 020 & 0xff; } sta…

But given you’re using 2 [IntList](https://processing.org/reference/IntList.html) containers already, and seems like the values within both _x_ & _y_ containers are within `short` and even the `byte` range, I’ve come up w/ a hackish solution which merges both containers as 1 container w/ same [**size()**](https://processing.org/reference/IntList_size_.html) by using the bitshift operators [`<<`](https://processing.org/reference/leftshift.html) & [`>>`](https://processing.org/reference/rightshift.html); then call method [**sort()**](https://processing.org/reference/IntList_sort_.html) and then split the single container back to **x** & **y** containers:

```java
/**
 * IntList XY Merge (v1.0.0)
 * GoToLoop (2025/Jan/15)
 * https://Discourse.Processing.org/t/how-to-sort-cordinates/45599/4
 */

final IntList
  x = new IntList(5, 3, 2, 4, 5), 
  y = new IntList(5, 4, 5, 4, 4);

final int[] xyArr = new int[2];

void setup() {
  println("input x:", x);
  println("input y:", y);

  final IntList xy = mergeXYLists(x, y, xyArr);
  println("unsorted xy:", xy);

  xy.sort();
  println("sorted xy:", xy);

  splitXYLists(xy, x, y, xyArr);
  println("sorted x:", x);
  println("sorted y:", y);

  exit();
}

static final int xyMerge(final int... xy) {
  return xy[0] << 16 | (char)xy[1];
}

static final int[] xySplit(final int xy) {
  return new int[] { xy >> 16, (short)(xy & 0xffff) };
}

static final int[] xySplit(final int xy, final int[] split) {
  split[0] = xy >> 16;
  split[1] = (short)(xy & 0xffff);
  return split;
}

static final IntList mergeXYLists(final IntList x, final IntList y) {
  return mergeXYLists(x, y, new int[2]);
}

static final IntList mergeXYLists
  (final IntList x, final IntList y, final int[] tmp)
{
  final int len = x.size();
  final IntList xy = new IntList(len);

  for (int i = 0; i < len; ++i) {
    tmp[0] = x.get(i);
    tmp[1] = y.get(i);
    xy.append(xyMerge(tmp));
  }

  return xy;
}

static final void splitXYLists
  (final IntList xy, final IntList x, final IntList y)
{
  splitXYLists(xy, x, y, new int[2]);
}

static final void splitXYLists
  (final IntList xy, final IntList x, final IntList y, final int[] tmp)
{
  final int len = xy.size();

  for (int i = 0; i < len; ++i) {
    xySplit(xy.get(i), tmp);
    x.set(i, tmp[0]);
    y.set(i, tmp[1]);
  }
}

```

```auto
input x: IntList size=5 [5, 3, 2, 4, 5]
input y: IntList size=5 [5, 4, 5, 4, 4]
unsorted xy: IntList size=5 [327685, 196612, 131077, 262148, 327684]
sorted xy: IntList size=5 [131077, 196612, 262148, 327684, 327685]
sorted x: IntList size=5 [2, 3, 4, 5, 5]
sorted y: IntList size=5 [5, 4, 4, 4, 5]

```

---

_[View the full topic](https://discourse.processing.org/t/how-to-sort-coordinates/45599)._
