Do you want to first add a row number to each line, then sort, then fold the identical lines?

so, if your starting data is:

AAAAA

BBBBB

AAAAA

VFVDS

AAAAA

then you'd first transform it to:

AAAAA|1

BBBBB|2

AAAAA|3

VFVDS|4

AAAAA|5

Then you'd sort it to:

AAAAA|1

AAAAA|3

AAAAA|5

BBBBB|2

VFVDS|4

Then you'd fold it to:

AAAAA|1|3|5

BBBBB|2

VFVDS|4

Is that the process you are looking for?