Forum Discussion
JackLacava
Honored Contributor
That's neat. It could be further generalized by accepting a parameterized switch (SortOrder=<human/lcg>) and another one to switch between ordering by name or description (SortBy=<name/desc>).
RobbSalzmann
12 months agoValued Contributor II
Or, if you want something quick and LINQ-y, these will work to sort lists of MemberInfo:
''VB.net Public Sub AlphaNumericSort(ByRef members as List(Of MemberInfo) Dim sortedMembers As List(Of MemberInfo) = members _ .OrderBy(Function(memberInfo) _ If(Regex.Match(memberInfo.Member.Name, "\d+").Success, _ 'Evaluation, starts with a number? Integer.Parse(Regex.Match(memberInfo.Member.Name, "\d+").Value), _ 'True Part Integer.MaxValue)) _ 'False Part, put after all that start with a number .ToList() End Sub // C#: public void AlphaNumericSort(ref List members) { List sortedMembers = members .OrderBy ( memberInfo => Regex.Match(memberInfo.Member.Name, @"\d+").Success ? //Evaluation, starts with a number? int.Parse(Regex.Match(memberInfo.Member.Name, @"\d+").Value) : //True int.MaxValue //False, put after all that start with a number ).ToList(); }
Used like this:
Public Class MainClass
Public Function Main(si As SessionInfo, globals As BRGlobals, api As Object, args As ExtenderArgs) As Object
Try
Dim mbrLst As New MemberList()
Dim mbrDisplayOptions As MemberDisplayOptions = New MemberDisplayOptions() _
With{ .IncludeAllProperties = True, .IncludeMemberInfoForProperties = True}
Dim lstMembers As List(Of MemberInfo) =
BRApi.Finance.Metadata.GetMembersUsingFilter(si, "CorpAccounts", "A#[GAAP Account Structure].Base", True, Nothing, mbrDisplayOptions)
'' Sort the members:
AlphaNumericSort(lstMembers)
mbrLst.AddMemberInfosToList(lstMembers)
Return mbrLst
Catch ex As Exception
Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
End Try
End Function
Public Sub AlphaNumericSort(ByRef members as List(Of MemberInfo)
Dim sortedMembers As List(Of MemberInfo) = members _
.OrderBy(Function(memberInfo) _
If(Regex.Match(memberInfo.Member.Name, "\d+").Success, _ 'Evaluation, starts with a number?
Integer.Parse(Regex.Match(memberInfo.Member.Name, "\d+").Value), _ 'True Part
Integer.MaxValue)) _ 'False Part, put after all that start with a number
.ToList()
End Sub
End Class
Related Content
- 4 months ago