0
$\begingroup$

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:

  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.

$\endgroup$
7
  • $\begingroup$ sort[#,weights]&/@needsreorder or sort[#[[2]],weights]&/@needsreorder? Please give example of your raw needsorder $\endgroup$
    – BlacKow
    Apr 15, 2016 at 17:15
  • $\begingroup$ @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 $\endgroup$ Apr 15, 2016 at 18:09
  • $\begingroup$ 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. $\endgroup$
    – BlacKow
    Apr 15, 2016 at 18:12
  • $\begingroup$ @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}} $\endgroup$ Apr 15, 2016 at 18:19
  • $\begingroup$ 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". $\endgroup$ Apr 15, 2016 at 18:23

1 Answer 1

3
$\begingroup$

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"}} *)
$\endgroup$
4
  • $\begingroup$ Yes I am! This performs elegantly. $\endgroup$ Apr 15, 2016 at 19:14
  • $\begingroup$ If your file is huge, you probably want to consider performance issues. $\endgroup$
    – BlacKow
    Apr 15, 2016 at 19:14
  • $\begingroup$ I'd love to throw 100k lines at it but, at this point, I'm happy with 50. $\endgroup$ Apr 15, 2016 at 19:38
  • $\begingroup$ 100k should be no problem $\endgroup$
    – BlacKow
    Apr 15, 2016 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.