Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This is my first post so apologies if any obvious mistakes.

I'm trying to work on a data containing names (in quotes) in Mathematica. The data is already saved on my system under names.txt. I would like to be able to sort these names in alphabetical order but I can't seem to do anything with it in Mathematica. None of the commands seem to be working and the file itself is not recognized as a string. I'm sure I'm making an obvious mistake in my code but would greatly appreciate any help. Here's my code below:

Original file (reduced version-original file has more than 1000 names): "MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON"

data1 = {Import["/Users/Joeblogs/Documents/mathematica/names.txt","string"]}

the output looks like:

{"JOHN", "DAVID", "VIVIAN", "ROBERT"}

How do I sort these in alphabetical order?

Thanks for your help.

share|improve this question
1  
What if you use "String"? –  J. M. Jun 3 '13 at 14:50
    
Do you mean String (with the "s" in capital)?. I tried that, it's still the same. When I use the "Sort" function, there's no change to the imported data. Thanks for your reply. –  John Jun 3 '13 at 14:56
2  
To sort you can use: Sort[{"JOHN", "DAVID", "VIVIAN", "ROBERT"}] –  user21 Jun 3 '13 at 14:57
3  
does that work: data1 = StringSplit[StringReplace[Import["names.txt"], "\"" -> ""], ","]? (and then Sort) –  Pinguin Dirk Jun 3 '13 at 15:10
1  
@John so, it seems I was right--you are importing the whole lot as a single string. Import as "CSV" instead of "String" and the correct result will be obtained. –  Oleksandr R. Jun 3 '13 at 15:21

1 Answer 1

up vote 5 down vote accepted

I think you want to use ReadList here instead of Import as it gives you lower level control. For the sake of the demo, I converted your example to a string, and using StringToStream converted it to an InputStream. In actuality, you can use ReadList directly on the filename.

strm = StringToStream[
  "\"MARY\",\"PATRICIA\",\"LINDA\",\"BARBARA\",\"ELIZABETH\", \
  "\JENNIFER\",\"MARIA\",\"SUSAN\",\"MARGARET\",\"DOROTHY\",\"LISA\", \
  "\NANCY\",\"KAREN\",\"BETTY\",\"HELEN\",\"SANDRA\",\"DONNA\",\"CAROL\", \
  \"RUTH\",\"SHARON\""
]

The key is to set the separators in ReadList, so it can do the processing for you:

ReadList[strm, Word, WordSeparators -> {" ", ",", "\t"}]

which returns

{"\"MARY\"", "\"PATRICIA\"", "\"LINDA\"", "\"BARBARA\"", 
 "\"ELIZABETH\"", "\"JENNIFER\"", "\"MARIA\"", "\"SUSAN\"", "\"MARGARET\"", 
 "\"DOROTHY\"", "\"LISA\"", "\"NANCY\"", "\"KAREN\"", "\"BETTY\"", 
 "\"HELEN\"", "\"SANDRA\"", "\"DONNA\"", "\"CAROL\"", "\"RUTH\"", 
 "\"SHARON\""}

Close[strm]

In your case, replace strm with the filename.

share|improve this answer
    
thank you rcollyer, your code works very well and it gives the output in string format. Brilliant! Thanks for the explanation too! –  John Jun 3 '13 at 15:26
    
@John ReadList is my usual default for this type of thing. Occasionally, I will use Import, but quite often I deal with formats that aren't supported by it, although I have written my own filters for it before. However, for simple things, I tend not to bother. –  rcollyer Jun 3 '13 at 15:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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