22// Licensed under the MIT License.
33
44using Microsoft . PowerShell . PSResourceGet . UtilClasses ;
5+ using NuGet . Versioning ;
56using System ;
67using System . Collections . Generic ;
8+ using System . Linq ;
79using System . Xml ;
810
911namespace Microsoft . PowerShell . PSResourceGet . Cmdlets
@@ -70,6 +72,9 @@ public override IEnumerable<PSResourceResult> ConvertToPSResourceResult(FindResu
7072 #region V2 Specific Methods
7173
7274 public XmlNode [ ] ConvertResponseToXML ( string httpResponse ) {
75+ NuGetVersion emptyVersion = new NuGetVersion ( "0.0.0.0" ) ;
76+ NuGetVersion firstVersion = emptyVersion ;
77+ NuGetVersion lastVersion = emptyVersion ;
7378
7479 //Create the XmlDocument.
7580 XmlDocument doc = new XmlDocument ( ) ;
@@ -80,7 +85,48 @@ public XmlNode[] ConvertResponseToXML(string httpResponse) {
8085 XmlNode [ ] nodes = new XmlNode [ entryNode . Count ] ;
8186 for ( int i = 0 ; i < entryNode . Count ; i ++ )
8287 {
83- nodes [ i ] = entryNode [ i ] ;
88+ XmlNode node = entryNode [ i ] ;
89+ nodes [ i ] = node ;
90+ var entryChildNodes = node . ChildNodes ;
91+ foreach ( XmlElement childNode in entryChildNodes )
92+ {
93+ var entryKey = childNode . LocalName ;
94+ if ( entryKey . Equals ( "properties" ) )
95+ {
96+ var propertyChildNodes = childNode . ChildNodes ;
97+ foreach ( XmlElement propertyChild in propertyChildNodes )
98+ {
99+ var propertyKey = propertyChild . LocalName ;
100+ var propertyValue = propertyChild . InnerText ;
101+ if ( propertyKey . Equals ( "NormalizedVersion" ) )
102+ {
103+ if ( ! NuGetVersion . TryParse ( propertyValue , out NuGetVersion parsedNormalizedVersion ) )
104+ {
105+ parsedNormalizedVersion = emptyVersion ;
106+ }
107+
108+ if ( i == 0 )
109+ {
110+ firstVersion = parsedNormalizedVersion ;
111+ }
112+ else
113+ {
114+ // later version element
115+ lastVersion = parsedNormalizedVersion ;
116+ }
117+
118+ break ; // don't care about rest of the childNode's properties
119+ }
120+ }
121+ }
122+ }
123+ }
124+
125+ // only order the array in desc order if array has more than 1 element and is currently in ascending order
126+ // check for emptyVersion is in case a version that couldn't be parsed was found, just keep ordering as is.
127+ if ( nodes . Length > 1 && firstVersion != emptyVersion && lastVersion != emptyVersion && firstVersion < lastVersion )
128+ {
129+ nodes = nodes . Reverse ( ) . ToArray ( ) ;
84130 }
85131
86132 return nodes ;
0 commit comments