torta
torta

Reputation: 33

TCL custom sorting

I would appreciate any help on this as I am new to TCL. I created a list of strings by doing a 'regexp -all -line -inline' + criterion on the output of a CLI command. Each element of this list now ends with a number and I want to sort the list on this particular numeric ending in each string but preserve the rest of the string. A close example would be to have to sort the output of the 'ls -la' command by the size of the files. I tried the following but it did not work:

lsort -command  "regexp -lineanchor {\d+$}" -integer $list

After spending a day on trying to figure this out I decided to ask you guys. Would you be able to help?

Upvotes: 3

Views: 1014

Answers (1)

bmk
bmk

Reputation: 14137

To be honest, I don't understand what you want to achieve with -command "regexp -lineanchor {\d+$}". The command regexp -lineanchor {\d+$} should actually always return 1 if you plan to compare numbers.

If you want to sort a list by the last element of its sublists you can use the -index option. E.g.:

lsort -index end -integer {{x y 5} {a b 8} {c c 3} {u u 1} {x y 2}}

returns:

{u u 1} {x y 2} {c c 3} {x y 5} {a b 8}

If you don't have your data in sublists but have the data line by line you have to split it before, e.g.:

lsort -index end -integer [split $data "\n"]

Upvotes: 5

Related Questions