@@ -183,21 +183,24 @@ func getFieldValueForByFieldValue(n *Node) *Value {
183183 return n .Values [0 ]
184184}
185185
186- // ByFieldValue is a NodeLess function that orders adjacent scalar nodes with the same name by
187- // their scalar value.
188- func ByFieldValue (_ , ni , nj * Node , isWholeSlice bool ) bool {
189- if isWholeSlice {
190- return false
191- }
192- vi := getFieldValueForByFieldValue (ni )
193- vj := getFieldValueForByFieldValue (nj )
194- if vi == nil {
195- return vj != nil
196- }
197- if vj == nil {
198- return false
186+ // ByFieldValue returns a NodeLess function that orders adjacent scalar nodes
187+ // with the same name by their scalar value. The values are passed through
188+ // `projection` before sorting.
189+ func ByFieldValue (projection func (string ) string ) NodeLess {
190+ return func (_ , ni , nj * Node , isWholeSlice bool ) bool {
191+ if isWholeSlice {
192+ return false
193+ }
194+ vi := getFieldValueForByFieldValue (ni )
195+ vj := getFieldValueForByFieldValue (nj )
196+ if vi == nil {
197+ return vj != nil
198+ }
199+ if vj == nil {
200+ return false
201+ }
202+ return projection (vi .Value ) < projection (vj .Value )
199203 }
200- return vi .Value < vj .Value
201204}
202205
203206func getChildValueByFieldSubfield (field , subfield string , n * Node ) * Value {
@@ -245,8 +248,9 @@ func ByFieldSubfield(field, subfield string) NodeLess {
245248
246249// ByFieldSubfieldPath returns a NodeLess function that orders adjacent message nodes with the given
247250// field name by the given subfield path value. If no field name is provided, it compares the
248- // subfields of any adjacent nodes with matching names.
249- func ByFieldSubfieldPath (field string , subfieldPath []string ) NodeLess {
251+ // subfields of any adjacent nodes with matching names. Values are passed
252+ // through `projection` before sorting.
253+ func ByFieldSubfieldPath (field string , subfieldPath []string , projection func (string ) string ) NodeLess {
250254 return func (_ , ni , nj * Node , isWholeSlice bool ) bool {
251255 if isWholeSlice {
252256 return false
@@ -259,7 +263,7 @@ func ByFieldSubfieldPath(field string, subfieldPath []string) NodeLess {
259263 if vj == nil {
260264 return false
261265 }
262- return vi .Value < vj .Value
266+ return projection ( vi .Value ) < projection ( vj .Value )
263267 }
264268}
265269
0 commit comments