# Sorting Arrays of Objects

**URL:** <https://forum.juce.com/t/sorting-arrays-of-objects/30980>\
**Category:** General JUCE discussion\
**Created:** [December 22, 2018, 8:47am UTC](https://forum.juce.com/t/sorting-arrays-of-objects/30980 "2018-12-22T08:47:01Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [August 15, 2019, 9:07am UTC](https://forum.juce.com/t/sorting-arrays-of-objects/30980/21 "2019-08-15T09:07:06Z")

</div>

Can you show the declaration of the Array and how you call sort, so people can check?

---

<div class="post-metadata">

**Author:** ![WilliamkWusik](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/williamkwusik/32/3821_2.png) [@WilliamkWusik](https://forum.juce.com/u/WilliamkWusik)\
**Post date:** [August 15, 2019, 5:22pm UTC](https://forum.juce.com/t/sorting-arrays-of-objects/30980/22 "2019-08-15T17:22:03Z")

</div>

Just the basic stuff.

```
OwnedArray<WLoopPart> theArray;

WLoopPartElementComparator sorter
theArray.sort(sorter);

```

Something like that. I already erased the code and will do my own sorter. ☹

But thanks for the help. (hug)

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [August 15, 2019, 5:27pm UTC](https://forum.juce.com/t/sorting-arrays-of-objects/30980/23 "2019-08-15T17:27:35Z")

</div>

I would assume OwnedArray contains pointers, so a comparison function/object that takes in references for the elements to compare isn’t going to work.

---

<div class="post-metadata">

**Author:** ![cpr2323](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/cpr2323/32/18682_2.png) [@cpr2323](https://forum.juce.com/u/cpr2323)\
**Post date:** [August 15, 2019, 5:36pm UTC](https://forum.juce.com/t/sorting-arrays-of-objects/30980/24 "2019-08-15T17:36:44Z")

</div>

From the OwnedArray docs…

```auto
Sorts the elements in the array.

This will use a comparator object to sort the elements into order. The object passed must have a method of the form:

int compareElements (ElementType* first, ElementType* second);

```

---

<div class="post-metadata">

**Author:** ![WilliamkWusik](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/williamkwusik/32/3821_2.png) [@WilliamkWusik](https://forum.juce.com/u/WilliamkWusik)\
**Post date:** [August 15, 2019, 5:47pm UTC](https://forum.juce.com/t/sorting-arrays-of-objects/30980/25 "2019-08-15T17:47:08Z")

</div>

(facepalm) (shameface) thanks guys… I’m getting old faster… :-p

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [November 19, 2025, 9:11am UTC](https://forum.juce.com/t/sorting-arrays-of-objects/30980/26 "2025-11-19T09:11:26Z")

</div>

don’t let Jules see that 🙂

[Previous page](https://forum.juce.com/t/sorting-arrays-of-objects/30980.md?page=1)
