Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting list

31 views
Skip to first unread message

hss...@googlemail.com

unread,
May 2, 2019, 10:32:22 AM5/2/19
to
Hi newsgroup,

I have the following code:

set mylist "{AN14 DDR-A.DM1} {AP18 DDR-A.DQ9} {AN19 DDR-A.DQ7} {AN16 DDR-A.DQ8} {AM17 DDR-A.DQ10} {AN17 DDR-A.DQS_C_1}"

puts [lsort -stride 2 -index 1 $mylist]


I get the following result:
{AN19 DDR-A.DQ7} {AN16 DDR-A.DQ8} {AM17 DDR-A.DQ10} {AN17 DDR-A.DQS_C_1} {AN14 DDR-A.DM1} {AP18 DDR-A.DQ9}


Why is the order different from ...DQ7, ...DQ8, ...DQ9, ...DQ10 ?

Thank you for your answers.


Cheers, hssig

Christian Gollwitzer

unread,
May 2, 2019, 11:26:36 AM5/2/19
to
Am 02.05.19 um 16:32 schrieb hss...@googlemail.com:
You have two misunderstandings (I think):

1) -stride is used if you have a list of pairs, not a list of list.
i.e. if your list looks like "x1 y1 x2 y2 ..." you can treat this as a
list of pairs with -stride 2. You have a list of sublists, i.e.

"{x1 y1} {x2 y2} ..."

and there you don't need nor want -stride, because "-index 1" will sort
everything according to the "y" values.

2) Still, if you try with -index 1:

% puts [lsort -index 1 $mylist]
{AN14 DDR-A.DM1} {AM17 DDR-A.DQ10} {AN19 DDR-A.DQ7} {AN16 DDR-A.DQ8}
{AP18 DDR-A.DQ9} {AN17 DDR-A.DQS_C_1}

the order is now 10 7 8 9

and this is because lsort performs a string comparison by default, 1 < 7
and therefore also 10 < 7. Because it is a common requirement to sort
embedded numbers numerically, lsort contains the "dictionary" sort mode:

% puts [lsort -index 1 -dictionary $mylist]
{AN14 DDR-A.DM1} {AN19 DDR-A.DQ7} {AN16 DDR-A.DQ8} {AP18 DDR-A.DQ9}
{AM17 DDR-A.DQ10} {AN17 DDR-A.DQS_C_1}

which should be an approximation of what you probably want.

Christian

Robert Heller

unread,
May 2, 2019, 11:45:46 AM5/2/19
to
From the lsort man page:

-ascii Use string comparison with Unicode code-point col-
lation order (the name is for backward-compatibil-
ity reasons.) This is the default.

-dictionary Use dictionary-style comparison. This is the same
as -ascii except (a) case is ignored except as a
tie-breaker and (b) if two strings contain embedded
numbers, the numbers compare as integers, not char-
acters. For example, in -dictionary mode, bigBoy
sorts between bigbang and bigboy, and x10y sorts
between x9y and x11y.

-integer Convert list elements to integers and use integer
comparison.

-real Convert list elements to floating-point values and
use floating comparison.

-command command Use command as a comparison command. To compare
two elements, evaluate a Tcl script consisting of
command with the two elements appended as addi-
tional arguments. The script should return an
integer less than, equal to, or greater than zero
if the first element is to be considered less than,
equal to, or greater than the second, respectively.



>
> Thank you for your answers.
>
>
> Cheers, hssig
>

--
Robert Heller -- 978-544-6933
Deepwoods Software -- Custom Software Services
http://www.deepsoft.com/ -- Linux Administration Services
hel...@deepsoft.com -- Webhosting Services

hss...@googlemail.com

unread,
May 2, 2019, 12:13:25 PM5/2/19
to
Hi Christian,

that works fine for me.

Thank you! Cheers, hssig
0 new messages