# Variant Compare Help

**URL:** <https://forum.xojo.com/t/variant-compare-help/20423>\
**Category:** General\
**Created:** [November 19, 2014, 4:05pm UTC](https://forum.xojo.com/t/variant-compare-help/20423 "2014-11-19T16:05:55Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brock\_Nash](https://forum.xojo.com/user_avatar/forum.xojo.com/brock_nash/32/1126_2.png) [@Brock\_Nash](https://forum.xojo.com/u/Brock_Nash)\
**Post date:** [November 19, 2014, 4:05pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/1 "2014-11-19T16:05:55Z")

</div>

I’m looking at the documentation on variants and it says

> [@](#):
>
> Variants used in an expression convert themselves to the type of the other operand, no matter what kind of data they contain, instead of deferring the type-conversion until runtime. This only affects expressions involving a variant operand and an operand of some other type, not expressions in which both operands are variants.

But what happens when both operands are variants?  
Essentially I want if the variants are strings, to do a string compare but if they’re integers or doubles I want it to instead compare their values.

So far I’m just trying to case it out

```auto
if str(v.DoubleValue) = v.StringValue and str(v2.DoubleValue) = v2.StringValue // then Compare their double values
```

```auto
elseif str(v.IntegerValue) = v.StringValue and str(v2.IntegerValue) = v2.StringValue // then Compare their integer values
```

Is this necessary?

I also see that there’s an Operator\_Compare method on the variant object but no examples of how to utilize this.  
I’m trying to do a merge sort on an array of variants essentially, but I need to get the Compare working correctly.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [November 19, 2014, 5:01pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/2 "2014-11-19T17:01:28Z")

</div>

IMO its never a good idea to compare two doubles for equality.

is 100.998726252443 the same as 100.9987262524429 ?

You could use format() here to force a certain number of decimal places

```auto
if format(v,"0.00") = format(v2,"0.00")
```

Comparing them as integers:  
would this be better?

```auto
if v.IntegerValue = v2.IntegerValue
```

since that already forces the conversion to integer?

---

<div class="post-metadata">

**Author:** ![Brock\_Nash](https://forum.xojo.com/user_avatar/forum.xojo.com/brock_nash/32/1126_2.png) [@Brock\_Nash](https://forum.xojo.com/u/Brock_Nash)\
**Post date:** [November 19, 2014, 5:09pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/3 "2014-11-19T17:09:32Z")

</div>

> [@144446:@Jeff Tullin](#):
>
> IMO its never a good idea to compare two doubles for equality.

I’m not comparing to check equality. I’m comparing so I can sort them into order. It’s actually more helpful if two doubles dont equal each other.

---

<div class="post-metadata">

**Author:** ![Bob\_Keeney](https://forum.xojo.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@Bob\_Keeney](https://forum.xojo.com/u/Bob_Keeney)\
**Post date:** [November 19, 2014, 5:50pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/4 "2014-11-19T17:50:12Z")

</div>

Variants are evil and should be used as sparingly as possible.

If you absolutely MUST use variants, check for their datatype, do an explicit conversion and compare them that way.

---

<div class="post-metadata">

**Author:** ![Will\_Shank](https://forum.xojo.com/letter_avatar_proxy/v4/letter/w/f475e1/32.png) [@Will\_Shank](https://forum.xojo.com/u/Will_Shank)\
**Post date:** [November 19, 2014, 6:05pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/5 "2014-11-19T18:05:15Z")

</div>

> [@144450:@Bob Keeney](#):
>
> the new Auto data type.

Are you leaking there’s going to be type inference?

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [November 19, 2014, 6:52pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/6 "2014-11-19T18:52:17Z")

</div>

NO  
There won’t be

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [November 19, 2014, 7:06pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/7 "2014-11-19T19:06:43Z")

</div>

If you use variants and rely on auto conversions then you get whatever “magic” or pain that brings along  
Use explicit type accessors

---

<div class="post-metadata">

**Author:** ![Brock\_Nash](https://forum.xojo.com/user_avatar/forum.xojo.com/brock_nash/32/1126_2.png) [@Brock\_Nash](https://forum.xojo.com/u/Brock_Nash)\
**Post date:** [November 19, 2014, 8:08pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/8 "2014-11-19T20:08:09Z")

</div>

Essentially I have an bunch of extension methods that work on arrays of objects.

```auto
myArray.Where("Width",">","400").Limit(20).OrderBy("Name")
```

I have a helper method that will turn arrays of primitives into arrays of (variant) objects so that they can be used with these extension methods, but implementing the OrderBy is difficult due to the fact that you don’t know what original type the variants came from and whether to sort them based on Value or String Order.

I wasn’t sure if there a good way to handle this or what the default comparing of variants would do.

---

<div class="post-metadata">

**Author:** ![Will\_Shank](https://forum.xojo.com/letter_avatar_proxy/v4/letter/w/f475e1/32.png) [@Will\_Shank](https://forum.xojo.com/u/Will_Shank)\
**Post date:** [November 19, 2014, 8:53pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/9 "2014-11-19T20:53:52Z")

</div>

Could you refactor to wrap the Variant array into a Class rather than working with a raw Variant Array.

With a class you can add a property to track the original array type which the helper methods would set. Then orderBy() could check that. And your extension methods could be moved to the class.

`Class FlexiList
  list() As Variant
  sourceType As something
End Class`

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [November 19, 2014, 9:00pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/10 "2014-11-19T21:00:46Z")

</div>

Variant.TypeString TypeInteger etc tells you what the original type was

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:29pm UTC](https://forum.xojo.com/t/variant-compare-help/20423/11 "2020-10-29T17:29:46Z")

</div>


