Sorting upon the first element of a nested list and show that as numerical values












6












$begingroup$


I wish to sort the below list upon the first elements:



 {{1/2 (-1 - Sqrt[2]), {{1, 11}, {1, 12}, {2, 11}}}, 
{-1 - Sqrt[2], {{5, 11}, {5, 12}}},
{1/2 (1 - Sqrt[2]), {{1, 9}, {1, 10}}},
{1 - Sqrt[2], {{7, 9}, {7, 10}}},
{1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}


The desired output can be shaped as:



{{-2.414, {{5, 11}, {5, 12}}}, 
{-1.207, {{1, 11}, {1, 12},{2,11}}},
{-0.414, {{7, 9}, {7, 10}}},
{-0.207, {{1, 9}, {1,10}}},
{0.207, {{1, 7}, {1, 8}}}}


where the first elements are numeric values (without Sqrt symbols) and just for 3-digit precision.
I have tried with N function. But the problem gives rise the effects on any pairs for example output be shaped as {-2.41421, {{5., 11.}, {5., 12.}}} (with a dot after 5 or 11.
Another problem is related to sorting of list just based on the first elements and NOT other elements.










share|improve this question











$endgroup$












  • $begingroup$
    If you want to keep the exact forms of the first element you can use SortBy[lst, First[N[#]]&]
    $endgroup$
    – N.J.Evans
    May 16 at 17:43
















6












$begingroup$


I wish to sort the below list upon the first elements:



 {{1/2 (-1 - Sqrt[2]), {{1, 11}, {1, 12}, {2, 11}}}, 
{-1 - Sqrt[2], {{5, 11}, {5, 12}}},
{1/2 (1 - Sqrt[2]), {{1, 9}, {1, 10}}},
{1 - Sqrt[2], {{7, 9}, {7, 10}}},
{1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}


The desired output can be shaped as:



{{-2.414, {{5, 11}, {5, 12}}}, 
{-1.207, {{1, 11}, {1, 12},{2,11}}},
{-0.414, {{7, 9}, {7, 10}}},
{-0.207, {{1, 9}, {1,10}}},
{0.207, {{1, 7}, {1, 8}}}}


where the first elements are numeric values (without Sqrt symbols) and just for 3-digit precision.
I have tried with N function. But the problem gives rise the effects on any pairs for example output be shaped as {-2.41421, {{5., 11.}, {5., 12.}}} (with a dot after 5 or 11.
Another problem is related to sorting of list just based on the first elements and NOT other elements.










share|improve this question











$endgroup$












  • $begingroup$
    If you want to keep the exact forms of the first element you can use SortBy[lst, First[N[#]]&]
    $endgroup$
    – N.J.Evans
    May 16 at 17:43














6












6








6





$begingroup$


I wish to sort the below list upon the first elements:



 {{1/2 (-1 - Sqrt[2]), {{1, 11}, {1, 12}, {2, 11}}}, 
{-1 - Sqrt[2], {{5, 11}, {5, 12}}},
{1/2 (1 - Sqrt[2]), {{1, 9}, {1, 10}}},
{1 - Sqrt[2], {{7, 9}, {7, 10}}},
{1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}


The desired output can be shaped as:



{{-2.414, {{5, 11}, {5, 12}}}, 
{-1.207, {{1, 11}, {1, 12},{2,11}}},
{-0.414, {{7, 9}, {7, 10}}},
{-0.207, {{1, 9}, {1,10}}},
{0.207, {{1, 7}, {1, 8}}}}


where the first elements are numeric values (without Sqrt symbols) and just for 3-digit precision.
I have tried with N function. But the problem gives rise the effects on any pairs for example output be shaped as {-2.41421, {{5., 11.}, {5., 12.}}} (with a dot after 5 or 11.
Another problem is related to sorting of list just based on the first elements and NOT other elements.










share|improve this question











$endgroup$




I wish to sort the below list upon the first elements:



 {{1/2 (-1 - Sqrt[2]), {{1, 11}, {1, 12}, {2, 11}}}, 
{-1 - Sqrt[2], {{5, 11}, {5, 12}}},
{1/2 (1 - Sqrt[2]), {{1, 9}, {1, 10}}},
{1 - Sqrt[2], {{7, 9}, {7, 10}}},
{1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}


The desired output can be shaped as:



{{-2.414, {{5, 11}, {5, 12}}}, 
{-1.207, {{1, 11}, {1, 12},{2,11}}},
{-0.414, {{7, 9}, {7, 10}}},
{-0.207, {{1, 9}, {1,10}}},
{0.207, {{1, 7}, {1, 8}}}}


where the first elements are numeric values (without Sqrt symbols) and just for 3-digit precision.
I have tried with N function. But the problem gives rise the effects on any pairs for example output be shaped as {-2.41421, {{5., 11.}, {5., 12.}}} (with a dot after 5 or 11.
Another problem is related to sorting of list just based on the first elements and NOT other elements.







list-manipulation numerics sorting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 16 at 20:01









Unbelievable

2,240931




2,240931










asked May 16 at 9:47









Inzo BabariaInzo Babaria

51629




51629












  • $begingroup$
    If you want to keep the exact forms of the first element you can use SortBy[lst, First[N[#]]&]
    $endgroup$
    – N.J.Evans
    May 16 at 17:43


















  • $begingroup$
    If you want to keep the exact forms of the first element you can use SortBy[lst, First[N[#]]&]
    $endgroup$
    – N.J.Evans
    May 16 at 17:43
















$begingroup$
If you want to keep the exact forms of the first element you can use SortBy[lst, First[N[#]]&]
$endgroup$
– N.J.Evans
May 16 at 17:43




$begingroup$
If you want to keep the exact forms of the first element you can use SortBy[lst, First[N[#]]&]
$endgroup$
– N.J.Evans
May 16 at 17:43










2 Answers
2






active

oldest

votes


















9












$begingroup$

SortBy[MapAt[N, lst, {All, 1}], First]



{{-2.41421, {{5, 11}, {5, 12}}}, {-1.20711, {{1, 11}, {1, 12}, {2,
11}}}, {-0.414214, {{7, 9}, {7, 10}}}, {-0.207107, {{1, 9}, {1,
10}}}, {0.207107, {{1, 7}, {1, 8}}}}




SortBy[N @* First] @ lst



{{-1 - Sqrt[2], {{5, 11}, {5, 12}}}, {1/2 (-1 - Sqrt[2]), {{1,
11}, {1, 12}, {2, 11}}}, {1 -
Sqrt[2], {{7, 9}, {7, 10}}}, {1/2 (1 - Sqrt[2]), {{1, 9}, {1,
10}}}, {1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}







share|improve this answer











$endgroup$













  • $begingroup$
    Thankxxx. It is very professional solution. Can you write the SortBy[N @* First] @ lst in a simpler way to understand how it does work.
    $endgroup$
    – Inzo Babaria
    May 16 at 10:04










  • $begingroup$
    I could not understand the mean of @*
    $endgroup$
    – Inzo Babaria
    May 16 at 10:05






  • 1




    $begingroup$
    @Inzo, it is short form for composition Composition[N, First], N@*First is the same function as N[First@#]&.
    $endgroup$
    – kglr
    May 16 at 10:12



















6












$begingroup$

list={{1/2 (-1 - Sqrt[2]), {{1, 11}, {1, 12}, {2, 11}}},{-1 - Sqrt[2], {{5, 11}, {5, 12}}},{1/2 (1 - Sqrt[2]), {{1, 9}, {1, 10}}}, {1 - Sqrt[2], {{7, 9}, {7, 10}}},{1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}   

SortBy[N@list,First]



{{-2.41421, {{5., 11.}, {5., 12.}}}, {-1.20711, {{1., 11.}, {1.,
12.}, {2., 11.}}}, {-0.414214, {{7., 9.}, {7.,
10.}}}, {-0.207107, {{1., 9.}, {1., 10.}}}, {0.207107, {{1.,
7.}, {1., 8.}}}}







share|improve this answer











$endgroup$














    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "387"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f198470%2fsorting-upon-the-first-element-of-a-nested-list-and-show-that-as-numerical-value%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9












    $begingroup$

    SortBy[MapAt[N, lst, {All, 1}], First]



    {{-2.41421, {{5, 11}, {5, 12}}}, {-1.20711, {{1, 11}, {1, 12}, {2,
    11}}}, {-0.414214, {{7, 9}, {7, 10}}}, {-0.207107, {{1, 9}, {1,
    10}}}, {0.207107, {{1, 7}, {1, 8}}}}




    SortBy[N @* First] @ lst



    {{-1 - Sqrt[2], {{5, 11}, {5, 12}}}, {1/2 (-1 - Sqrt[2]), {{1,
    11}, {1, 12}, {2, 11}}}, {1 -
    Sqrt[2], {{7, 9}, {7, 10}}}, {1/2 (1 - Sqrt[2]), {{1, 9}, {1,
    10}}}, {1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}







    share|improve this answer











    $endgroup$













    • $begingroup$
      Thankxxx. It is very professional solution. Can you write the SortBy[N @* First] @ lst in a simpler way to understand how it does work.
      $endgroup$
      – Inzo Babaria
      May 16 at 10:04










    • $begingroup$
      I could not understand the mean of @*
      $endgroup$
      – Inzo Babaria
      May 16 at 10:05






    • 1




      $begingroup$
      @Inzo, it is short form for composition Composition[N, First], N@*First is the same function as N[First@#]&.
      $endgroup$
      – kglr
      May 16 at 10:12
















    9












    $begingroup$

    SortBy[MapAt[N, lst, {All, 1}], First]



    {{-2.41421, {{5, 11}, {5, 12}}}, {-1.20711, {{1, 11}, {1, 12}, {2,
    11}}}, {-0.414214, {{7, 9}, {7, 10}}}, {-0.207107, {{1, 9}, {1,
    10}}}, {0.207107, {{1, 7}, {1, 8}}}}




    SortBy[N @* First] @ lst



    {{-1 - Sqrt[2], {{5, 11}, {5, 12}}}, {1/2 (-1 - Sqrt[2]), {{1,
    11}, {1, 12}, {2, 11}}}, {1 -
    Sqrt[2], {{7, 9}, {7, 10}}}, {1/2 (1 - Sqrt[2]), {{1, 9}, {1,
    10}}}, {1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}







    share|improve this answer











    $endgroup$













    • $begingroup$
      Thankxxx. It is very professional solution. Can you write the SortBy[N @* First] @ lst in a simpler way to understand how it does work.
      $endgroup$
      – Inzo Babaria
      May 16 at 10:04










    • $begingroup$
      I could not understand the mean of @*
      $endgroup$
      – Inzo Babaria
      May 16 at 10:05






    • 1




      $begingroup$
      @Inzo, it is short form for composition Composition[N, First], N@*First is the same function as N[First@#]&.
      $endgroup$
      – kglr
      May 16 at 10:12














    9












    9








    9





    $begingroup$

    SortBy[MapAt[N, lst, {All, 1}], First]



    {{-2.41421, {{5, 11}, {5, 12}}}, {-1.20711, {{1, 11}, {1, 12}, {2,
    11}}}, {-0.414214, {{7, 9}, {7, 10}}}, {-0.207107, {{1, 9}, {1,
    10}}}, {0.207107, {{1, 7}, {1, 8}}}}




    SortBy[N @* First] @ lst



    {{-1 - Sqrt[2], {{5, 11}, {5, 12}}}, {1/2 (-1 - Sqrt[2]), {{1,
    11}, {1, 12}, {2, 11}}}, {1 -
    Sqrt[2], {{7, 9}, {7, 10}}}, {1/2 (1 - Sqrt[2]), {{1, 9}, {1,
    10}}}, {1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}







    share|improve this answer











    $endgroup$



    SortBy[MapAt[N, lst, {All, 1}], First]



    {{-2.41421, {{5, 11}, {5, 12}}}, {-1.20711, {{1, 11}, {1, 12}, {2,
    11}}}, {-0.414214, {{7, 9}, {7, 10}}}, {-0.207107, {{1, 9}, {1,
    10}}}, {0.207107, {{1, 7}, {1, 8}}}}




    SortBy[N @* First] @ lst



    {{-1 - Sqrt[2], {{5, 11}, {5, 12}}}, {1/2 (-1 - Sqrt[2]), {{1,
    11}, {1, 12}, {2, 11}}}, {1 -
    Sqrt[2], {{7, 9}, {7, 10}}}, {1/2 (1 - Sqrt[2]), {{1, 9}, {1,
    10}}}, {1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}








    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 16 at 10:02

























    answered May 16 at 9:57









    kglrkglr

    198k10223449




    198k10223449












    • $begingroup$
      Thankxxx. It is very professional solution. Can you write the SortBy[N @* First] @ lst in a simpler way to understand how it does work.
      $endgroup$
      – Inzo Babaria
      May 16 at 10:04










    • $begingroup$
      I could not understand the mean of @*
      $endgroup$
      – Inzo Babaria
      May 16 at 10:05






    • 1




      $begingroup$
      @Inzo, it is short form for composition Composition[N, First], N@*First is the same function as N[First@#]&.
      $endgroup$
      – kglr
      May 16 at 10:12


















    • $begingroup$
      Thankxxx. It is very professional solution. Can you write the SortBy[N @* First] @ lst in a simpler way to understand how it does work.
      $endgroup$
      – Inzo Babaria
      May 16 at 10:04










    • $begingroup$
      I could not understand the mean of @*
      $endgroup$
      – Inzo Babaria
      May 16 at 10:05






    • 1




      $begingroup$
      @Inzo, it is short form for composition Composition[N, First], N@*First is the same function as N[First@#]&.
      $endgroup$
      – kglr
      May 16 at 10:12
















    $begingroup$
    Thankxxx. It is very professional solution. Can you write the SortBy[N @* First] @ lst in a simpler way to understand how it does work.
    $endgroup$
    – Inzo Babaria
    May 16 at 10:04




    $begingroup$
    Thankxxx. It is very professional solution. Can you write the SortBy[N @* First] @ lst in a simpler way to understand how it does work.
    $endgroup$
    – Inzo Babaria
    May 16 at 10:04












    $begingroup$
    I could not understand the mean of @*
    $endgroup$
    – Inzo Babaria
    May 16 at 10:05




    $begingroup$
    I could not understand the mean of @*
    $endgroup$
    – Inzo Babaria
    May 16 at 10:05




    1




    1




    $begingroup$
    @Inzo, it is short form for composition Composition[N, First], N@*First is the same function as N[First@#]&.
    $endgroup$
    – kglr
    May 16 at 10:12




    $begingroup$
    @Inzo, it is short form for composition Composition[N, First], N@*First is the same function as N[First@#]&.
    $endgroup$
    – kglr
    May 16 at 10:12











    6












    $begingroup$

    list={{1/2 (-1 - Sqrt[2]), {{1, 11}, {1, 12}, {2, 11}}},{-1 - Sqrt[2], {{5, 11}, {5, 12}}},{1/2 (1 - Sqrt[2]), {{1, 9}, {1, 10}}}, {1 - Sqrt[2], {{7, 9}, {7, 10}}},{1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}   

    SortBy[N@list,First]



    {{-2.41421, {{5., 11.}, {5., 12.}}}, {-1.20711, {{1., 11.}, {1.,
    12.}, {2., 11.}}}, {-0.414214, {{7., 9.}, {7.,
    10.}}}, {-0.207107, {{1., 9.}, {1., 10.}}}, {0.207107, {{1.,
    7.}, {1., 8.}}}}







    share|improve this answer











    $endgroup$


















      6












      $begingroup$

      list={{1/2 (-1 - Sqrt[2]), {{1, 11}, {1, 12}, {2, 11}}},{-1 - Sqrt[2], {{5, 11}, {5, 12}}},{1/2 (1 - Sqrt[2]), {{1, 9}, {1, 10}}}, {1 - Sqrt[2], {{7, 9}, {7, 10}}},{1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}   

      SortBy[N@list,First]



      {{-2.41421, {{5., 11.}, {5., 12.}}}, {-1.20711, {{1., 11.}, {1.,
      12.}, {2., 11.}}}, {-0.414214, {{7., 9.}, {7.,
      10.}}}, {-0.207107, {{1., 9.}, {1., 10.}}}, {0.207107, {{1.,
      7.}, {1., 8.}}}}







      share|improve this answer











      $endgroup$
















        6












        6








        6





        $begingroup$

        list={{1/2 (-1 - Sqrt[2]), {{1, 11}, {1, 12}, {2, 11}}},{-1 - Sqrt[2], {{5, 11}, {5, 12}}},{1/2 (1 - Sqrt[2]), {{1, 9}, {1, 10}}}, {1 - Sqrt[2], {{7, 9}, {7, 10}}},{1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}   

        SortBy[N@list,First]



        {{-2.41421, {{5., 11.}, {5., 12.}}}, {-1.20711, {{1., 11.}, {1.,
        12.}, {2., 11.}}}, {-0.414214, {{7., 9.}, {7.,
        10.}}}, {-0.207107, {{1., 9.}, {1., 10.}}}, {0.207107, {{1.,
        7.}, {1., 8.}}}}







        share|improve this answer











        $endgroup$



        list={{1/2 (-1 - Sqrt[2]), {{1, 11}, {1, 12}, {2, 11}}},{-1 - Sqrt[2], {{5, 11}, {5, 12}}},{1/2 (1 - Sqrt[2]), {{1, 9}, {1, 10}}}, {1 - Sqrt[2], {{7, 9}, {7, 10}}},{1/2 (-1 + Sqrt[2]), {{1, 7}, {1, 8}}}}   

        SortBy[N@list,First]



        {{-2.41421, {{5., 11.}, {5., 12.}}}, {-1.20711, {{1., 11.}, {1.,
        12.}, {2., 11.}}}, {-0.414214, {{7., 9.}, {7.,
        10.}}}, {-0.207107, {{1., 9.}, {1., 10.}}}, {0.207107, {{1.,
        7.}, {1., 8.}}}}








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 16 at 10:23

























        answered May 16 at 9:57









        J42161217J42161217

        5,425525




        5,425525






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Mathematica Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid …



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f198470%2fsorting-upon-the-first-element-of-a-nested-list-and-show-that-as-numerical-value%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bruad Bilen | Luke uk diar | NawigatsjuunCommonskategorii: BruadCommonskategorii: RunstükenWikiquote: Bruad

            Modern approach to radio buttons

            A word for worn pool table felt