0

I have a text file in this format:

19-2 : (28, 33, 40),
15-3 : (28, 33, 43),
12-4 : (28, 33, 45),
9-5 : (28, 33, 47),
8-6 : (28, 33, 48),
5-7 : (28, 33, 50),
3-8 : (28, 33, 52),
0-9 : (28, 33, 55),
0-10 : (28, 33, 57),
etc.

I'd like to use python to sort lines in ascending order by the first number and then by the number after the "-". Any tips you can give me on how to approach this problem?

Thanks!

EDIT: I should have mentioned, these files are millions of lines long, so I can't actually store them in memory...

  • 2
    What have you tried so far? SO is not a code-writing service; we are here to help you with specific questions about code you've already written, or to help you out of a bind if you're stuck. Please show some effort, and it will be rewarded with help from the community. – MattDMo Aug 25 '14 at 22:39
  • What do you mean by the first number? e.g. on the first line, is the number you're looking at `19` or `28`? – mgilson Aug 25 '14 at 22:40
  • Matt, I'm sure you get a lot of people asking you to write code for them here, but that's not actually what I'm looking for, and I don't think my question is at all inappropriate. As a VERY new, self-taught programer I sometimes approach a problem like this and have NO IDEA where to start, so a little guidance, such as what Katrina offered, goes a long way towards making me a better, more helpful member of this community. :) –  Aug 26 '14 at 03:32
  • mgilson, yeah, so the first number on that first line would be 19, and if there are more than one 19 before the dash I want the lines organised in ascending order by the number directly after the dash, e.g. 19-2, 19-3, 19-17 etc. –  Aug 26 '14 at 03:34

1 Answers1

0

You can read the text from the file, parse it into, maybe a list of tuples, then sort the list based on a tuple index. When that's done, you can output it back to a file.

katerinah
  • 111
  • 4
  • Thanks Katerina! That helps a lot. In that first line I want to be using the 19-2 as my index (if this were a dictionary). Does that count as a tuple? In the tutorials I've been following I've only seen tuples like this: (28, 33, 40)... The files I'm sorting are millions of lines long, so I actually can't store them in memory at all. I guess that would have been an important thing to mention above.... –  Aug 26 '14 at 03:37
  • 1
    You can look at this answer: http://stackoverflow.com/questions/14465154/sorting-text-file-by-using-python – katerinah Aug 27 '14 at 04:51