sorting by sub-sub-list elements 
Author Message
 sorting by sub-sub-list elements

I have a list like this:
{
    {a {3 y}}
    {b {2 x}}
    {c {1 z}}

Quote:
}

I want the list sorted to become:
{
    {b {2 x}}
    {a {3 y}}
    {c {1 z}}

Quote:
}

I probably should use the -command option of lsort, but the reference is
a bit vague on how to do this...


Sat, 21 Sep 2002 03:00:00 GMT  
 sorting by sub-sub-list elements
Quote:

> I have a list like this:
> {
>     {a {3 y}}
>     {b {2 x}}
>     {c {1 z}}
> }

> I want the list sorted to become:
> {
>     {b {2 x}}
>     {a {3 y}}
>     {c {1 z}}
> }

> I probably should use the -command option of lsort, but the reference is
> a bit vague on how to do this...

set L {
    {a {3 y}}
    {b {2 x}}
    {c {1 z}}
Quote:
}

# The sort command (must have 2 args and return -1/0/+1):
proc sort-ssl {x y} {
        string compare [lindex [lindex $x 1] 1]\
         [lindex [lindex $y 1] 1]
Quote:
}

# the invocation:
lsort -command sort-ssl $L
{b {2 x}} {a {3 y}} {c {1 z}}

Note that your formatting with newlines and indents is not preserved,
since lsort produces a new list.
--
Schoene Gruesse/best regards, Richard Suchenwirth - +49-7531-86 2703
RC DT2, Siemens Electrocom, Buecklestr. 1-5, D-78467 Konstanz,Germany
-------------- http://purl.org/thecliff/tcl/wiki//Richard*Suchenwirth
AL:The Analytical Engine worketh not! CB:What version dost thou have?



Sat, 21 Sep 2002 03:00:00 GMT  
 sorting by sub-sub-list elements
Probably the simplest way is to build the sort key by prefixing each
list element with the required sort key. Do the sort and then strip
afterwards. Prefixing and stripping are both linear operations that do
not impinge on the n(log n) behaviour inside lsort, and save lsort
having to call "outside itself" everytime it does a test. It also
optimises the way lsort expects to make the comparison.
Quote:

> I have a list like this:
> {
>     {a {3 y}}
>     {b {2 x}}
>     {c {1 z}}
> }

> I want the list sorted to become:
> {
>     {b {2 x}}
>     {a {3 y}}
>     {c {1 z}}
> }

> I probably should use the -command option of lsort, but the reference is
> a bit vague on how to do this...



Sat, 21 Sep 2002 03:00:00 GMT  
 sorting by sub-sub-list elements

Quote:

> I have a list like this:
> {
>     {a {3 y}}
>     {b {2 x}}
>     {c {1 z}}
> }

> I want the list sorted to become:
> {
>     {b {2 x}}
>     {a {3 y}}
>     {c {1 z}}
> }

> I probably should use the -command option of lsort, but the reference is
> a bit vague on how to do this...

I don't know if the is the "correct" way but it works

proc mysort {a b} {
        string compare [lindex [lindex $a 1] 1] [lindex [lindex $b 1] 1]]

Quote:
}

set mylist [list {a {3 y}} {b {2 x}} {c {1 z}}]
puts $mylist
puts [lsort -command mysort $mylist]

Gordon



Sat, 21 Sep 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. $(TELEGEN2)/lib/rt.sub versus $(TELEGEN2)/lib/h_rt.sub

2. Sub-Totals on List Box

3. Sub-Dir/Values from Browse Lists

4. mail list sub

5. fast sub list operations

6. FW: fast sub list operations

7. sharing data in list sub-classes

8. Can a C++ sub-program invoked as a cin node call a LabView sub-vi as a "function"

9. sub

10. slow creation of sub-presenters

11. MVS/JES2/WTO(R)/CONSOLE AS/MPF exit/SUB SYSTEM

12. sub

 

 
Powered by phpBB® Forum Software