| | 1 | | using Microsoft.Xrm.Sdk; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | |
|
| | 5 | | namespace FakeXrmEasy |
| | 6 | | { |
| | 7 | | public class XrmOrderByAttributeComparer : IComparer<object> |
| | 8 | | { |
| | 9 | | public int Compare(Object objectA, Object objectB) |
| 1480 | 10 | | { |
| 1736 | 11 | | if (objectA == null && objectB == null) return 0; //Equal |
| | 12 | |
|
| 1224 | 13 | | if (objectA == null) |
| 0 | 14 | | return -1; |
| 1224 | 15 | | if (objectB == null) |
| 6 | 16 | | return 1; |
| | 17 | |
|
| 1218 | 18 | | Type attributeType = objectA.GetType(); |
| | 19 | |
|
| 1218 | 20 | | if (attributeType == typeof(OptionSetValue)) |
| 12 | 21 | | { |
| | 22 | | // we'll want the text value |
| 12 | 23 | | OptionSetValue attributeValueA = (OptionSetValue)(objectA); |
| 12 | 24 | | OptionSetValue attributeValueB = (OptionSetValue)(objectB); |
| 12 | 25 | | return attributeValueA.Value.CompareTo(attributeValueB.Value); |
| | 26 | | } |
| 1206 | 27 | | else if (attributeType == typeof(EntityReference) |
| 1206 | 28 | | #if FAKE_XRM_EASY |
| 1206 | 29 | | || attributeType == typeof(Microsoft.Xrm.Client.CrmEntityReference) |
| 1206 | 30 | | #endif |
| 1206 | 31 | | ) |
| 84 | 32 | | { |
| | 33 | | // Name might well be Null in an entity reference? |
| 84 | 34 | | EntityReference entityRefA = (EntityReference)objectA; |
| 84 | 35 | | EntityReference entityRefB = (EntityReference)objectB; |
| | 36 | |
|
| 96 | 37 | | if (entityRefA.Name == null && entityRefB.Name == null) return 0; //Equal |
| | 38 | |
|
| 72 | 39 | | if (entityRefA.Name == null) |
| 0 | 40 | | return -1; |
| 72 | 41 | | if (entityRefB.Name == null) |
| 12 | 42 | | return 1; |
| | 43 | |
|
| 60 | 44 | | return entityRefA.Name.CompareTo(entityRefB.Name); |
| | 45 | | } |
| 1122 | 46 | | else if (attributeType == typeof(Money)) |
| 30 | 47 | | { |
| 30 | 48 | | Decimal valueA = ((Money)objectA).Value; |
| 30 | 49 | | Decimal valueB = ((Money)objectB).Value; |
| 30 | 50 | | var x = valueA.CompareTo(valueB); |
| 30 | 51 | | return x; |
| | 52 | | } |
| 1092 | 53 | | else if (attributeType == typeof(string)) |
| 6 | 54 | | { |
| 6 | 55 | | return String.Compare(objectA.ToString(), objectB.ToString()); |
| | 56 | | } |
| 1086 | 57 | | else if (attributeType == typeof(int)) |
| 888 | 58 | | { |
| 888 | 59 | | return ((int)objectA).CompareTo(((int)objectB)); |
| | 60 | | } |
| 198 | 61 | | else if (attributeType == typeof(DateTime)) |
| 36 | 62 | | { |
| 36 | 63 | | return ((DateTime)objectA).CompareTo((DateTime)objectB); |
| | 64 | | } |
| 162 | 65 | | else if (attributeType == typeof(Guid)) |
| 12 | 66 | | { |
| 12 | 67 | | return ((Guid)objectA).CompareTo((Guid)objectB); |
| | 68 | | } |
| 150 | 69 | | else if (attributeType == typeof(decimal)) |
| 12 | 70 | | { |
| 12 | 71 | | return ((decimal)objectA).CompareTo((decimal)objectB); |
| | 72 | | } |
| 138 | 73 | | else if (attributeType == typeof(double)) |
| 12 | 74 | | { |
| 12 | 75 | | return ((double)objectA).CompareTo((double)objectB); |
| | 76 | | } |
| 126 | 77 | | else if (attributeType == typeof(float)) |
| 12 | 78 | | { |
| 12 | 79 | | return ((float)objectA).CompareTo((float)objectB); |
| | 80 | | } |
| 114 | 81 | | else if (attributeType == typeof(bool)) |
| 12 | 82 | | { |
| 12 | 83 | | return ((bool)objectA).CompareTo((bool)objectB); |
| | 84 | | } |
| 102 | 85 | | else if (attributeType == typeof(AliasedValue)) |
| 102 | 86 | | { |
| 102 | 87 | | return Compare((objectA as AliasedValue)?.Value, (objectB as AliasedValue)?.Value); |
| | 88 | | } |
| | 89 | | else |
| 0 | 90 | | { |
| 0 | 91 | | return 0; |
| | 92 | | } |
| 1480 | 93 | | } |
| | 94 | | } |
| | 95 | | } |