How to sort a string

Hi All,

I’ve a string a single string like example “FeeName: Tuition-S1_2024, FromDate: 2/12/2024, Invoice Amount: 8250, FeeName: Tuition-S2_2024,FromDate: 7/29/2024, Invoice Amount: 8250, FeeName: Tuition-S1_2025, FromDate: 2/10/2025, Invoice Amount: 8250, FeeName: Tuition-S2_2022, FromDate: 7/25/2022, Invoice Amount: 0, FeeName: Tuition-S1_2023, FromDate: 2/13/2023, Invoice Amount: 0, FeeName: Tuition-S1_2023, FromDate: 2/13/2023, Invoice Amount: 0, FeeName: Tuition-S2_2023, FromDate: 7/24/2023, Invoice Amount: 8250”

I want to rearrange that string into an order based on From Date like below
FeeName: Tuition-S2_2022, FromDate: 7/25/2022, Invoice Amount: 0, FeeName: Tuition-S1_2023, FromDate: 2/13/2023, Invoice Amount: 0,FeeName: Tuition-S1_2023, FromDate: 2/13/2023,FeeName: Tuition-S2_2023, FromDate: 7/24/2023, Invoice Amount: 8250,FeeName: Tuition-S1_2024, FromDate: 2/12/2024, Invoice Amount: 8250,FeeName: Tuition-S2_2024,FromDate: 7/29/2024, Invoice Amount: 8250,FeeName: Tuition-S1_2025, FromDate: 2/10/2025, Invoice Amount: 8250,

Basically with latest From date comes first , how to do that any help would be greatful.

Hi,

Hope the following sample helps you.

image

mc = System.Text.RegularExpressions.Regex.Matches(yourString,"FeeName:\s*Tuition-([A-Za-z\d]+)_(\d+).*?(?=FeeName|\s*$)")
String.Join(vbCrLf,mc.Cast(Of System.Text.RegularExpressions.Match).OrderBy(Function(m) m.Groups(2).Value).ThenBy(Function(m) m.Groups(1).Value).Select(Function(m) m.Value))

if you need comma as separator, please replace vbcrlf with “,” in the above expression.

Sample20220621-4.zip (2.4 KB)

Regards,

1 Like

Thanks a lot Yoichi.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.