# Applying multiple functions to a single column in a table

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"};
descriptive2={"Auto","Bin","Blower","Boiler"};
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:

1. StringSplit the second column (DESC).
2. Apply the sort function to the second column (DESC).
3. StringJoin the newly sorted entries(DESC).
4. Map the results of my sort function to the first column (PARTNUM).
5. 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 or sort[#[[2]],weights]&/@needsreorder? Please give example of your raw needsorder – BlacKow Apr 15 '16 at 17:15
• @BlacKow I do not feel confident either one of those is correct, but my desire is to apply the these functions to the text within the second column only. I did my best to provide a sample from my raw data here: Out//TableForm PARTNUM, DESC DUK175189, Blower 14 Hose, ROU4060436, Halogen 120 Volt 100 Watt Lamp – JonathanKolar Apr 15 '16 at 18:09
• Can't you try to check if it's correct ? Don't apply TableForm, just paste result of needsreorder[[1;;5]] Or put your test50parts.csv online and post a link to it. – BlacKow Apr 15 '16 at 18:12
• @BlacKow Oh, of course, my misunderstanding. 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}} – JonathanKolar Apr 15 '16 at 18:19
• applying sort[#,weights]&/@needsreorder sorts each line alphabetically by first letter, and sort[#[[2]],weights]&/@needsreorder comes back with an error: " Nonatomic expression expected at position 1". – JonathanKolar Apr 15 '16 at 18:23

I think you are looking for something like this:

ordered = {#[[1]], StringRiffle@sort[StringSplit@#[[2]], weights]} & /@
needsreorder
Export["ordered50parts.csv", ordered]
(*{{"PARTNUM", "DESC"}, {"DUK175189", "Blower Hose 14"}, {"ROU4060436",
"Halogen Lamp 100 120 Volt Watt"}} *)

• Yes I am! This performs elegantly. – JonathanKolar Apr 15 '16 at 19:14
• If your file is huge, you probably want to consider performance issues. – BlacKow Apr 15 '16 at 19:14
• I'd love to throw 100k lines at it but, at this point, I'm happy with 50. – JonathanKolar Apr 15 '16 at 19:38
• 100k should be no problem – BlacKow Apr 15 '16 at 19:40