PreviousUpNext

5.5.18  Sort a list of tuples or records

Sort a list of string-pairs by first element:

    eval:  sort  (\\ ((a,_),(b,_)) =  a > b)    [ ("abc","xyz"), ("xyz","abc"), ("jkl","mno") ];

    [("abc", "xyz"), ("jkl", "mno"), ("xyz", "abc")]

(The above will work no matter what the type of the the second element.)

Sort a list of string-pairs by second element:

    eval:  sort  (\\ ((_,a),(_,b)) =  a > b)    [ ("abc","xyz"), ("xyz","abc"), ("jkl","mno") ];

    [("xyz", "abc"), ("jkl", "mno"), ("abc", "xyz")]

(The above will work no matter what type the type of the first element.)

Sort a list of records by some field:

    eval:  sort  (\\ (a,b) =  a.key > b.key)    [ { key => "abc", value => "xyz" }, { key => "xyz", value => "abc" }, { key => "jkl", value => "mno" } ];

    [{ key="abc", value="xyz" }, 
     { key="jkl", value="mno" }, 
     { key="xyz", value="abc" }]

In a program (vs a script) you will want to replace sort by list_mergesort::sort.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext