Sorting a list according to some pre-specified rulesHow to Gather a list with some elements considered uniqueBest way to modify values in a list of rules?Creating a number based on given conditionsSorting a matrix alphanumericallyNIntegrate receiving a function with NumericQ which returns a listComponentwise Addition of TemporalDataFaster Integer Approximation of piTransformation syntax with shortcuts to a more explicit syntax for an equationGenerating a list of numbers that are not multiples of 4Extract values from a string list with string elements like “ Id=”1“ ”

How did the IEC decide to create kibibytes?

What is the shape of the upper boundary of water hitting a screen?

Can the Four Elements monk's Shape the Flowing River elemental discipline create stairs by expending a single ki point?

How was the website able to tell my credit card was wrong before it processed it?

Quotients of a ring of integers

Why did RFK loathe LBJ?

Intern not wearing safety equipment; how could I have handled this differently?

Why am I getting unevenly-spread results when using $RANDOM?

How many Jimmys can fit?

How can I review my manager, who is fine?

How do I talk to my wife about unrealistic expectations?

Category-theoretic treatment of diffs, patches and merging?

Is this car delivery via Ebay Motors on Craigslist a scam?

Is it acceptable that I plot a time-series figure with years increasing from right to left?

NOLOCK or Read Uncommitted locking / latching behaviours

How do resistors generate different heat if we make the current fixed and changed the voltage and resistance? Notice the flow of charge is constant

Shipped package arrived - didn't order, possible scam?

First road bike, decade old quality or lower end and new?

I'm feeling like my character doesn't fit the campaign

How to have a filled pattern

What factors could lead to bishops establishing monastic armies?

How to understand flavors and when to use combination of them?

Can we share mixing jug/beaker for developer, fixer and stop bath?

Troubling glyphs



Sorting a list according to some pre-specified rules


How to Gather a list with some elements considered uniqueBest way to modify values in a list of rules?Creating a number based on given conditionsSorting a matrix alphanumericallyNIntegrate receiving a function with NumericQ which returns a listComponentwise Addition of TemporalDataFaster Integer Approximation of piTransformation syntax with shortcuts to a more explicit syntax for an equationGenerating a list of numbers that are not multiples of 4Extract values from a string list with string elements like “ Id=”1“ ”






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3












$begingroup$


Given a list such as



12,9,3,42,30,10,11,1,2


I would like to place only the multiples of 3 in the natural order without changing the order of the other integers, so I would like to get as a result



42,30,12,10,11,9,3,1,2


What is a (simple) way to do this?



Thank you!
This is my first post and I tried to read related questions before posting.
I am just learning my way around in Mathematica. I am trying to write a little program.










share|improve this question









New contributor



StefanoK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$











  • $begingroup$
    Hi StefanoK, welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned.
    $endgroup$
    – rhermans
    7 hours ago










  • $begingroup$
    Please notice that accepting is one of the things to do after your question is answered, but we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
    $endgroup$
    – rhermans
    7 hours ago

















3












$begingroup$


Given a list such as



12,9,3,42,30,10,11,1,2


I would like to place only the multiples of 3 in the natural order without changing the order of the other integers, so I would like to get as a result



42,30,12,10,11,9,3,1,2


What is a (simple) way to do this?



Thank you!
This is my first post and I tried to read related questions before posting.
I am just learning my way around in Mathematica. I am trying to write a little program.










share|improve this question









New contributor



StefanoK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$











  • $begingroup$
    Hi StefanoK, welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned.
    $endgroup$
    – rhermans
    7 hours ago










  • $begingroup$
    Please notice that accepting is one of the things to do after your question is answered, but we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
    $endgroup$
    – rhermans
    7 hours ago













3












3








3





$begingroup$


Given a list such as



12,9,3,42,30,10,11,1,2


I would like to place only the multiples of 3 in the natural order without changing the order of the other integers, so I would like to get as a result



42,30,12,10,11,9,3,1,2


What is a (simple) way to do this?



Thank you!
This is my first post and I tried to read related questions before posting.
I am just learning my way around in Mathematica. I am trying to write a little program.










share|improve this question









New contributor



StefanoK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$




Given a list such as



12,9,3,42,30,10,11,1,2


I would like to place only the multiples of 3 in the natural order without changing the order of the other integers, so I would like to get as a result



42,30,12,10,11,9,3,1,2


What is a (simple) way to do this?



Thank you!
This is my first post and I tried to read related questions before posting.
I am just learning my way around in Mathematica. I am trying to write a little program.







list-manipulation






share|improve this question









New contributor



StefanoK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



StefanoK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 7 hours ago









rhermans

22.4k4 gold badges41 silver badges107 bronze badges




22.4k4 gold badges41 silver badges107 bronze badges






New contributor



StefanoK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 8 hours ago









StefanoKStefanoK

161 bronze badge




161 bronze badge




New contributor



StefanoK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




StefanoK is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.













  • $begingroup$
    Hi StefanoK, welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned.
    $endgroup$
    – rhermans
    7 hours ago










  • $begingroup$
    Please notice that accepting is one of the things to do after your question is answered, but we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
    $endgroup$
    – rhermans
    7 hours ago
















  • $begingroup$
    Hi StefanoK, welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned.
    $endgroup$
    – rhermans
    7 hours ago










  • $begingroup$
    Please notice that accepting is one of the things to do after your question is answered, but we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
    $endgroup$
    – rhermans
    7 hours ago















$begingroup$
Hi StefanoK, welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned.
$endgroup$
– rhermans
7 hours ago




$begingroup$
Hi StefanoK, welcome to Mma.SE. Start by taking the tour now and learning about asking and what's on-topic. Always edit if improvable, show due diligence, give brief context, include minimal working example of code and data in formatted form. By doing all this you help us to help you and likely you will inspire great answers. The site depends on participation, as you receive give back: vote and answer questions, keep the site useful, be kind, correct mistakes and share what you have learned.
$endgroup$
– rhermans
7 hours ago












$begingroup$
Please notice that accepting is one of the things to do after your question is answered, but we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
$endgroup$
– rhermans
7 hours ago




$begingroup$
Please notice that accepting is one of the things to do after your question is answered, but we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers.
$endgroup$
– rhermans
7 hours ago










2 Answers
2






active

oldest

votes


















3












$begingroup$

Answer



Sort[
12, 9, 3, 42, 30, 10, 11, 1, 2
, If[
Divisible[#1, 3] && Divisible[#2, 3]
, #2 < #1
, 0
] &
]
(* 42, 30, 12, 9, 3, 10, 11, 1, 2 *)


Explanation



Sort allows a second argument to define the ordering function.



Mathematica graphics



The ordering function takes two arguments #1 and #2 to be compared and normally should return either True or False. Sort also accepts it to return 0 when two elements should be treated as identical.



Mathematica graphics



So the strategy is to ask first If the numbers should be sorted using Divisible, and then return True or False if they should be sorted, or 0 otherwise.






share|improve this answer











$endgroup$




















    1












    $begingroup$

    This would be how I would think of it



    Extract multiples of 3



    Sort into decreasing order



    Extract non-multiples of 3



    Join those two results together



    and translate that into Mathematica



    v=12,9,3,42,30,10,11,1,2;
    mult3[x_]:=Mod[x,3]==0;
    notmult3[x_]:=Mod[x,3]!=0;
    Join[
    Sort[Select[v,mult3],Greater],
    Select[v,notmult3]
    ]


    giving



    42, 30, 12, 9, 3, 10, 11, 1, 2


    Look up each of those functions in the help system and see how much you can make of this.



    The usual Mathematica culture would turn all that into a single line and replace some of those names with abbreviations that are made up of a few punctuation characters. You can start learning that after you have figured out a little more about how to think in Mathematica.



    There are always multiple ways of doing anything in Mathematica. Pick a few simple ways to do things that you can remember how to correctly use.






    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
      );



      );






      StefanoK is a new contributor. Be nice, and check out our Code of Conduct.









      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f201614%2fsorting-a-list-according-to-some-pre-specified-rules%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









      3












      $begingroup$

      Answer



      Sort[
      12, 9, 3, 42, 30, 10, 11, 1, 2
      , If[
      Divisible[#1, 3] && Divisible[#2, 3]
      , #2 < #1
      , 0
      ] &
      ]
      (* 42, 30, 12, 9, 3, 10, 11, 1, 2 *)


      Explanation



      Sort allows a second argument to define the ordering function.



      Mathematica graphics



      The ordering function takes two arguments #1 and #2 to be compared and normally should return either True or False. Sort also accepts it to return 0 when two elements should be treated as identical.



      Mathematica graphics



      So the strategy is to ask first If the numbers should be sorted using Divisible, and then return True or False if they should be sorted, or 0 otherwise.






      share|improve this answer











      $endgroup$

















        3












        $begingroup$

        Answer



        Sort[
        12, 9, 3, 42, 30, 10, 11, 1, 2
        , If[
        Divisible[#1, 3] && Divisible[#2, 3]
        , #2 < #1
        , 0
        ] &
        ]
        (* 42, 30, 12, 9, 3, 10, 11, 1, 2 *)


        Explanation



        Sort allows a second argument to define the ordering function.



        Mathematica graphics



        The ordering function takes two arguments #1 and #2 to be compared and normally should return either True or False. Sort also accepts it to return 0 when two elements should be treated as identical.



        Mathematica graphics



        So the strategy is to ask first If the numbers should be sorted using Divisible, and then return True or False if they should be sorted, or 0 otherwise.






        share|improve this answer











        $endgroup$















          3












          3








          3





          $begingroup$

          Answer



          Sort[
          12, 9, 3, 42, 30, 10, 11, 1, 2
          , If[
          Divisible[#1, 3] && Divisible[#2, 3]
          , #2 < #1
          , 0
          ] &
          ]
          (* 42, 30, 12, 9, 3, 10, 11, 1, 2 *)


          Explanation



          Sort allows a second argument to define the ordering function.



          Mathematica graphics



          The ordering function takes two arguments #1 and #2 to be compared and normally should return either True or False. Sort also accepts it to return 0 when two elements should be treated as identical.



          Mathematica graphics



          So the strategy is to ask first If the numbers should be sorted using Divisible, and then return True or False if they should be sorted, or 0 otherwise.






          share|improve this answer











          $endgroup$



          Answer



          Sort[
          12, 9, 3, 42, 30, 10, 11, 1, 2
          , If[
          Divisible[#1, 3] && Divisible[#2, 3]
          , #2 < #1
          , 0
          ] &
          ]
          (* 42, 30, 12, 9, 3, 10, 11, 1, 2 *)


          Explanation



          Sort allows a second argument to define the ordering function.



          Mathematica graphics



          The ordering function takes two arguments #1 and #2 to be compared and normally should return either True or False. Sort also accepts it to return 0 when two elements should be treated as identical.



          Mathematica graphics



          So the strategy is to ask first If the numbers should be sorted using Divisible, and then return True or False if they should be sorted, or 0 otherwise.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 7 hours ago

























          answered 7 hours ago









          rhermansrhermans

          22.4k4 gold badges41 silver badges107 bronze badges




          22.4k4 gold badges41 silver badges107 bronze badges























              1












              $begingroup$

              This would be how I would think of it



              Extract multiples of 3



              Sort into decreasing order



              Extract non-multiples of 3



              Join those two results together



              and translate that into Mathematica



              v=12,9,3,42,30,10,11,1,2;
              mult3[x_]:=Mod[x,3]==0;
              notmult3[x_]:=Mod[x,3]!=0;
              Join[
              Sort[Select[v,mult3],Greater],
              Select[v,notmult3]
              ]


              giving



              42, 30, 12, 9, 3, 10, 11, 1, 2


              Look up each of those functions in the help system and see how much you can make of this.



              The usual Mathematica culture would turn all that into a single line and replace some of those names with abbreviations that are made up of a few punctuation characters. You can start learning that after you have figured out a little more about how to think in Mathematica.



              There are always multiple ways of doing anything in Mathematica. Pick a few simple ways to do things that you can remember how to correctly use.






              share|improve this answer









              $endgroup$

















                1












                $begingroup$

                This would be how I would think of it



                Extract multiples of 3



                Sort into decreasing order



                Extract non-multiples of 3



                Join those two results together



                and translate that into Mathematica



                v=12,9,3,42,30,10,11,1,2;
                mult3[x_]:=Mod[x,3]==0;
                notmult3[x_]:=Mod[x,3]!=0;
                Join[
                Sort[Select[v,mult3],Greater],
                Select[v,notmult3]
                ]


                giving



                42, 30, 12, 9, 3, 10, 11, 1, 2


                Look up each of those functions in the help system and see how much you can make of this.



                The usual Mathematica culture would turn all that into a single line and replace some of those names with abbreviations that are made up of a few punctuation characters. You can start learning that after you have figured out a little more about how to think in Mathematica.



                There are always multiple ways of doing anything in Mathematica. Pick a few simple ways to do things that you can remember how to correctly use.






                share|improve this answer









                $endgroup$















                  1












                  1








                  1





                  $begingroup$

                  This would be how I would think of it



                  Extract multiples of 3



                  Sort into decreasing order



                  Extract non-multiples of 3



                  Join those two results together



                  and translate that into Mathematica



                  v=12,9,3,42,30,10,11,1,2;
                  mult3[x_]:=Mod[x,3]==0;
                  notmult3[x_]:=Mod[x,3]!=0;
                  Join[
                  Sort[Select[v,mult3],Greater],
                  Select[v,notmult3]
                  ]


                  giving



                  42, 30, 12, 9, 3, 10, 11, 1, 2


                  Look up each of those functions in the help system and see how much you can make of this.



                  The usual Mathematica culture would turn all that into a single line and replace some of those names with abbreviations that are made up of a few punctuation characters. You can start learning that after you have figured out a little more about how to think in Mathematica.



                  There are always multiple ways of doing anything in Mathematica. Pick a few simple ways to do things that you can remember how to correctly use.






                  share|improve this answer









                  $endgroup$



                  This would be how I would think of it



                  Extract multiples of 3



                  Sort into decreasing order



                  Extract non-multiples of 3



                  Join those two results together



                  and translate that into Mathematica



                  v=12,9,3,42,30,10,11,1,2;
                  mult3[x_]:=Mod[x,3]==0;
                  notmult3[x_]:=Mod[x,3]!=0;
                  Join[
                  Sort[Select[v,mult3],Greater],
                  Select[v,notmult3]
                  ]


                  giving



                  42, 30, 12, 9, 3, 10, 11, 1, 2


                  Look up each of those functions in the help system and see how much you can make of this.



                  The usual Mathematica culture would turn all that into a single line and replace some of those names with abbreviations that are made up of a few punctuation characters. You can start learning that after you have figured out a little more about how to think in Mathematica.



                  There are always multiple ways of doing anything in Mathematica. Pick a few simple ways to do things that you can remember how to correctly use.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 7 hours ago









                  BillBill

                  6,5856 silver badges9 bronze badges




                  6,5856 silver badges9 bronze badges




















                      StefanoK is a new contributor. Be nice, and check out our Code of Conduct.









                      draft saved

                      draft discarded


















                      StefanoK is a new contributor. Be nice, and check out our Code of Conduct.












                      StefanoK is a new contributor. Be nice, and check out our Code of Conduct.











                      StefanoK is a new contributor. Be nice, and check out our Code of Conduct.














                      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%2f201614%2fsorting-a-list-according-to-some-pre-specified-rules%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

                      Sahara Skak | Bilen | Luke uk diar | NawigatsjuunCommonskategorii: SaharaWikivoyage raisfeerer: Sahara26° N, 13° O

                      The fall designs the understood secretary. Looking glass Science Shock Discovery Hot Everybody Loves Raymond Smile 곳 서비스 성실하다 Defas Kaloolon Definition: To combine or impregnate with sulphur or any of its compounds as to sulphurize caoutchouc in vulcanizing Flame colored Reason Useful Thin Help 갖다 유명하다 낙엽 장례식 Country Iron Definition: A fencer a gladiator one who exhibits his skill in the use of the sword Definition: The American black throated bunting Spiza Americana Nostalgic Needy Method to my madness 시키다 평가되다 전부 소설가 우아하다 Argument Tin Feeling Representative Gym Music Gaur Chicken 일쑤 코치 편 학생증 The harbor values the sugar. Vasagle Yammoe Enstatite Definition: Capable of being limited Road Neighborly Five Refer Built Kangaroo 비비다 Degree Release Bargain Horse 하루 형님 유교 석 동부 괴롭히다 경제력

                      19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу