@@ -10,6 +10,7 @@ This Source Code Form is subject to the terms of the
1010using OneScript . Values ;
1111using ScriptEngine . Machine . Contexts ;
1212using OneScript . Execution ;
13+ using OneScript . Types ;
1314
1415namespace ScriptEngine . Machine
1516{
@@ -18,6 +19,10 @@ public class GenericIValueComparer : IEqualityComparer<IValue>, IComparer<IValue
1819 private readonly IBslProcess _process ;
1920 private readonly Func < IValue , IValue , int > _comparer ;
2021
22+ static private readonly List < TypeDescriptor > orderedTypes = new List < TypeDescriptor >
23+ { BasicTypes . Undefined , BasicTypes . Null , BasicTypes . Boolean ,
24+ BasicTypes . Number , BasicTypes . String , BasicTypes . Date , BasicTypes . Type } ;
25+
2126 public GenericIValueComparer ( )
2227 {
2328 _comparer = CompareAsStrings ;
@@ -62,12 +67,28 @@ private int CompareByPresentations(IValue x, IValue y)
6267 return ( ( BslValue ) x ) . ToString ( _process ) . CompareTo ( ( ( BslValue ) y ) . ToString ( _process ) ) ;
6368 }
6469
70+ private int CompareByTypes ( IValue x , IValue y )
71+ {
72+ var ix = orderedTypes . IndexOf ( x . SystemType ) ;
73+ var iy = orderedTypes . IndexOf ( y . SystemType ) ;
74+
75+ if ( ix >= 0 )
76+ return ( iy >= 0 ) ? ix - iy : - 1 ;
77+ else if ( iy >= 0 )
78+ return 1 ;
79+
80+ return _comparer ( x , y ) ;
81+ }
82+
6583 public int Compare ( IValue x , IValue y )
6684 {
6785 if ( ReferenceEquals ( x , y ) )
6886 return 0 ;
6987
70- if ( x is IBslComparable && x . SystemType == y . SystemType )
88+ if ( x . SystemType != y . SystemType )
89+ return CompareByTypes ( x , y ) ;
90+
91+ if ( x is IBslComparable )
7192 return x . CompareTo ( y ) ;
7293 else
7394 return _comparer ( x , y ) ;
0 commit comments