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;
$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.
list-manipulation
New contributor
$endgroup$
add a comment |
$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.
list-manipulation
New contributor
$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
add a comment |
$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.
list-manipulation
New contributor
$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
list-manipulation
New contributor
New contributor
edited 7 hours ago
rhermans
22.4k4 gold badges41 silver badges107 bronze badges
22.4k4 gold badges41 silver badges107 bronze badges
New contributor
asked 8 hours ago
StefanoKStefanoK
161 bronze badge
161 bronze badge
New contributor
New contributor
$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
add a comment |
$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
add a comment |
2 Answers
2
active
oldest
votes
$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.
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.
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.
$endgroup$
add a comment |
$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.
$endgroup$
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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.
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.
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.
$endgroup$
add a comment |
$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.
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.
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.
$endgroup$
add a comment |
$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.
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.
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.
$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.
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.
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.
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
add a comment |
add a comment |
$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.
$endgroup$
add a comment |
$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.
$endgroup$
add a comment |
$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.
$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.
answered 7 hours ago
BillBill
6,5856 silver badges9 bronze badges
6,5856 silver badges9 bronze badges
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
$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