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:


How do I sort these in alphabetical order?

Thanks for your help.

  • 1
    $\begingroup$ What if you use "String"? $\endgroup$ – J. M. will be back soon Jun 3 '13 at 14:50
  • $\begingroup$ 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. $\endgroup$ – John_dydx Jun 3 '13 at 14:56
  • 2
    $\begingroup$ To sort you can use: Sort[{"JOHN", "DAVID", "VIVIAN", "ROBERT"}] $\endgroup$ – user21 Jun 3 '13 at 14:57
  • 3
    $\begingroup$ does that work: data1 = StringSplit[StringReplace[Import["names.txt"], "\"" -> ""], ","]? (and then Sort) $\endgroup$ – Pinguin Dirk Jun 3 '13 at 15:10
  • 1
    $\begingroup$ @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. $\endgroup$ – Oleksandr R. Jun 3 '13 at 15:21

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[
  "\NANCY\",\"KAREN\",\"BETTY\",\"HELEN\",\"SANDRA\",\"DONNA\",\"CAROL\", \

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\"", 


In your case, replace strm with the filename.

  • $\begingroup$ thank you rcollyer, your code works very well and it gives the output in string format. Brilliant! Thanks for the explanation too! $\endgroup$ – John_dydx Jun 3 '13 at 15:26
  • $\begingroup$ @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. $\endgroup$ – rcollyer Jun 3 '13 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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