77using System . Security . Cryptography ;
88using System . Text ;
99using System . Text . RegularExpressions ;
10- using System . Threading . Tasks ;
1110
1211namespace JsonLD
1312{
@@ -158,6 +157,14 @@ public static void Reverse<T>(this IList<T> list)
158157 }
159158 }
160159
160+ class JTokenStringCompare : Comparer < JToken >
161+ {
162+ public override int Compare ( JToken x , JToken y )
163+ {
164+ return string . Compare ( ( string ) x , ( string ) y ) ;
165+ }
166+ }
167+
161168 public static void SortInPlace < T > ( this IList < T > list )
162169 {
163170 if ( list is List < T > )
@@ -168,8 +175,10 @@ public static void SortInPlace<T>(this IList<T> list)
168175 {
169176 // TODO(sblom): This is really awful; figure out how to really sort a JArray in place.
170177 JArray arr = ( JArray ) list ;
171- var tmp = new List < JToken > ( ( JArray ) list ) ;
172- tmp . Sort ( Comparer < JToken > . Create ( ( s , t ) => string . Compare ( ( string ) s , ( string ) t ) ) ) ;
178+ // .Select(x => x) is a workaround for .NET 3.5's List constructor's failure to
179+ // disbelieve Newtonsoft.Json when IJCollection.Count returns 0.
180+ List < JToken > tmp = arr . Select ( x => x ) . ToList ( ) ;
181+ tmp . Sort ( new JTokenStringCompare ( ) ) ;
173182 arr . RemoveAll ( ) ;
174183 foreach ( var t in tmp )
175184 {
@@ -193,7 +202,9 @@ public static void SortInPlace<T>(this IList<T> list, IComparer<T> cmp)
193202 // TODO(sblom): This is really awful; figure out how to really sort a JArray in place.
194203 JArray arr = ( JArray ) list ;
195204 IComparer < JToken > comparer = ( IComparer < JToken > ) cmp ;
196- var tmp = new List < JToken > ( ( JArray ) list ) ;
205+ // .Select(x => x) is a workaround for .NET 3.5's List constructor's failure to
206+ // disbelieve Newtonsoft.Json when IJCollection.Count returns 0.
207+ var tmp = arr . Select ( x => x ) . ToList ( ) ;
197208 tmp . Sort ( comparer ) ;
198209 tmp . Select ( ( t , i ) => arr [ i ] = tmp [ i ] ) ;
199210 }
0 commit comments