I've seen similar questions, but I'm having trouble applying solutions found there to my situation.
My sorting function:
size={"Big", "Large", "Narrow", "Short"};
color={"Amber","Black","Blue","Brown"};
directional={"Back","Bottom","Center","End"};
material={"Aluminum","Brass","Bronze","Halogen"};
descriptive={"Access","Acorn","Adjustable","Blower"};
descriptive2={"Auto","Bin","Blower","Boiler"};
noun={"Actuator","Adapter","Hose","Lamp"};
other={"Assembly","Kit","Pack","Package"};
weights= Transpose@
Flatten[#, 1] &@(Transpose@{#1, ConstantArray[#2, Length@#1]}
& @@@ {{size,1},{color,2},{directional,3},{material,4},
{descriptive,5},{descriptive2,6},{noun,7},{other,8}});
sort[phrase_,weighs_]:= SortBy[phrase,
If[#=={},Infinity,First[#]]&@ Pick[weights[[2]],weights[[1]],#]&];
2 examples from the list of items I want to sort:
needsreorder=Import["test50parts.csv"];
needsreorder//TableForm
Out//TableForm
PARTNUM, DESC
DUK175189, Blower 14 Hose
ROU4060436, Halogen 120 Volt 100 Watt Lamp
Objectives for this problem:
StringSplit
the second column (DESC).- Apply the
sort
function to the second column (DESC). StringJoin
the newly sorted entries(DESC).- Map the results of my sort function to the first column (PARTNUM).
- Export a file with the two columns mapped together.
My sort function works with individual examples, but I'm trying to get to the next level of being able to do multiple lines at a time. My desired results are:
PARTNUM, DESC
DUK175189, Blower Hose 14
ROU4060436, Halogen Lamp 120 Volt 100 Watt
I'm aware of the apply function, but not sure how to use it with a list like this.
Apply[sort,needsreorder]
?
Major newbie here, thanks for your patience.
sort[#,weights]&/@needsreorder
orsort[#[[2]],weights]&/@needsreorder
? Please give example of your rawneedsorder
$\endgroup$Out//TableForm PARTNUM, DESC DUK175189, Blower 14 Hose, ROU4060436, Halogen 120 Volt 100 Watt Lamp
$\endgroup$TableForm
, just paste result ofneedsreorder[[1;;5]]
Or put your test50parts.csv online and post a link to it. $\endgroup$needsreorder[[1;;5]] = {{PARTNUM,DESC},{DUK175189,Blower 14 Hose},{ROU4060436,Halogen 120 Volt 100 Watt Lamp},{APW8400420,Thrust Bearing},{PTB8003106,Quick Disc 48 1Npt Pg}}
$\endgroup$sort[#,weights]&/@needsreorder
sorts each line alphabetically by first letter, andsort[#[[2]],weights]&/@needsreorder
comes back with an error: " Nonatomic expression expected at position 1". $\endgroup$