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 does the cloaker's Phantasms action work?

Troubling glyphs

What was the significance of Spider-Man: Far From Home being an MCU Phase 3 film instead of a Phase 4 film?

Custom Geolocation Fields not populating in test class

Is conquering your neighbors to fight a greater enemy a valid strategy?

Passwordless authentication - how and when to invalidate a login code

What is this burst transmission sequence across the entire band?

Why do Martians have to wear space helmets?

Why do people prefer metropolitan areas, considering monsters and villains?

Why did the frequency of the word "черт" (devil) in books increase by a few times since the October Revolution?

What are some bad ways to subvert tropes?

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

Why no parachutes in the Orion AA2 abort test?

Possibility to correct pitch from digital versions of records with the hole not centered

Is it ok for parents to kiss and romance with each other while their 2- to 8-year-old child watches?

How should I ask for a "pint" in countries that use metric?

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

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

Why is there paternal, for fatherly, fraternal, for brotherly, but no similar word for sons?

Why SQL does not use the indexed view?

Ways to demonstrate ("show-off") contributions as an undergraduate in research

What does the multimeter dial do internally?

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

Wouldn't putting an electronic key inside a small Faraday cage render it completely useless?



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 6 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

                      Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                      Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                      Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її