# ElementComparator

**URL:** <https://forum.juce.com/t/elementcomparator/131>\
**Category:** General JUCE discussion\
**Created:** [November 28, 2004, 1:15pm UTC](https://forum.juce.com/t/elementcomparator/131 "2004-11-28T13:15:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtxx000](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jtxx000](https://forum.juce.com/u/jtxx000)\
**Post date:** [November 28, 2004, 1:15pm UTC](https://forum.juce.com/t/elementcomparator/131/1 "2004-11-28T13:15:46Z")

</div>

Are there any examples on how to use this? Basically, I have an array of objects of a class I defined, and the class contains an int and I want to sort by the value of that int. How easy would this be?

---

<div class="post-metadata">

**Author:** ![SigmaDeltaRob](https://avatars.discourse-cdn.com/v4/letter/s/45deac/32.png) [@SigmaDeltaRob](https://forum.juce.com/u/SigmaDeltaRob)\
**Post date:** [November 29, 2004, 7:16pm UTC](https://forum.juce.com/t/elementcomparator/131/2 "2004-11-29T19:16:37Z")

</div>

Err…

---

<div class="post-metadata">

**Author:** ![jtxx000](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jtxx000](https://forum.juce.com/u/jtxx000)\
**Post date:** [November 29, 2004, 10:02pm UTC](https://forum.juce.com/t/elementcomparator/131/3 "2004-11-29T22:02:47Z")

</div>

eh?

---

<div class="post-metadata">

**Author:** ![jtxx000](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jtxx000](https://forum.juce.com/u/jtxx000)\
**Post date:** [December 10, 2004, 12:42am UTC](https://forum.juce.com/t/elementcomparator/131/4 "2004-12-10T00:42:30Z")

</div>

Ok, here’s what I’m trying right now that’s not working…  
i have a class SampleData with these variables:

```auto
	StringArray files;
	Array<int> velLayers;
	String name;
	int loKey;
	int highKey;
	int rootKey;
```

i have no idea how to use operators but i made a guess and came up with this:

```auto
	bool operator> (const SampleData& other) {
		return rootKey>other.rootKey;
	}

	bool operator< (const SampleData& other) {
		return rootKey<other.rootKey;
	}

	bool operator== (const SampleData& other) {
		return rootKey==other.rootKey;
	}
```

is this the right way to do it?

then i have an OwnedArray of type SampleData:

```auto
OwnedArray<SampleData> sampsL;
```

and i want to sort it so i tried this:  
`	FloatElementComparator<SampleData*> sorter;
	sampsL.sort(sorter);
`  
but it doesn’t work… i know this is probably my lack of experiance w/ c++ so any help as to where i went wrong would be appreciated… 🙂

---

<div class="post-metadata">

**Author:** ![NiallM](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/niallm/32/293_2.png) [@NiallM](https://forum.juce.com/u/NiallM)\
**Post date:** [December 10, 2004, 11:15am UTC](https://forum.juce.com/t/elementcomparator/131/5 "2004-12-10T11:15:13Z")

</div>

I might be wrong (I haven’t used FloatElementComparator myself), but isn’t the problem that FloatElementComparator is meant to only be used with floating point data types? I think what you probably want to do is write your own subclass of ElementComparator to handle your SampleData class.

(the operator stuff looks right to me, by the way)

- Niall.

---

<div class="post-metadata">

**Author:** ![jtxx000](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jtxx000](https://forum.juce.com/u/jtxx000)\
**Post date:** [December 10, 2004, 1:59pm UTC](https://forum.juce.com/t/elementcomparator/131/6 "2004-12-10T13:59:03Z")

</div>

well it compiles ok, it just doesn’t do anything. i took a look at the floatcomparator class and all it does is check to see if a \< b a==b or a\>b so shouldn’t that just use my custom operators?

---

<div class="post-metadata">

**Author:** ![NiallM](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/niallm/32/293_2.png) [@NiallM](https://forum.juce.com/u/NiallM)\
**Post date:** [December 10, 2004, 2:25pm UTC](https://forum.juce.com/t/elementcomparator/131/7 "2004-12-10T14:25:21Z")

</div>

Oh, I see what you mean :oops:. Could it perhaps be that you’re initialising the element comparator with a pointer instead of the actual data type then? i.e. it’s comparing two addresses instead of the actual class itself.

- Niall.

---

<div class="post-metadata">

**Author:** ![jtxx000](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jtxx000](https://forum.juce.com/u/jtxx000)\
**Post date:** [December 10, 2004, 11:18pm UTC](https://forum.juce.com/t/elementcomparator/131/8 "2004-12-10T23:18:22Z")

</div>

that makes sense, so if that’s the case then the easiest way to do it would probably be to subclass elementcomparator… here’s floatcomparator

```auto
00224 template <class ElementType>
00225 class FloatElementComparator : public ElementComparator<ElementType>
00226 {
00227 int compareElements (ElementType first,
00228 ElementType second)
00229 {
00230 return (first < second) ? -1
00231 : ((first == second) ? 0
00232 : 1);
00233 }
00234 };
```

so i have no idea how to change this to work with my type… in java you could just cast to your class and then perform the checks, is there a way to cast like this in c++?

---

<div class="post-metadata">

**Author:** ![NiallM](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/niallm/32/293_2.png) [@NiallM](https://forum.juce.com/u/NiallM)\
**Post date:** [December 12, 2004, 8:27pm UTC](https://forum.juce.com/t/elementcomparator/131/9 "2004-12-12T20:27:14Z")

</div>

Yeah, basically you just want to write a simple subclass like this:

`template <class ElementType>
 class NiallsElementComparator : public ElementComparator<ElementType>
 {
     int compareElements (ElementType first,
                          ElementType second)
     {
         return (*first < *second) ? -1
                                 : ((*first == *second) ? 0
                                                      : 1);
     }
 };`  
i.e. you’re comparing the contents of the object rather than the pointer to it (because first and second in this case are pointers to your SampleData objects, held by the sampsL OwnedArray).

- Niall.

---

<div class="post-metadata">

**Author:** ![jtxx000](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@jtxx000](https://forum.juce.com/u/jtxx000)\
**Post date:** [December 12, 2004, 11:12pm UTC](https://forum.juce.com/t/elementcomparator/131/10 "2004-12-12T23:12:06Z")

</div>

doh, i feel stupid 😛 … thank you 😃

---

<div class="post-metadata">

**Author:** ![NiallM](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/niallm/32/293_2.png) [@NiallM](https://forum.juce.com/u/NiallM)\
**Post date:** [December 13, 2004, 10:25am UTC](https://forum.juce.com/t/elementcomparator/131/11 "2004-12-13T10:25:14Z")

</div>

No problem 😃

- Niall.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 12, 2017, 9:51am UTC](https://forum.juce.com/t/elementcomparator/131/12 "2017-05-12T09:51:00Z")

</div>

Closed on May 12, 2017, 9:51am UTC


