Skip to content
Advertisement

txt file sorting(key:value in every line) – a problem with ‘n’

I am trying to sort txt file which looks like that :

JavaScript

and so on(+1000 lines), to that structure:

JavaScript

byr, iyr etc. order doesn’t matter, but every “set” of key:value has to be seperated by blank line. My main problem, if I can call it that way, is to create piece of code that sorts the file properly when there is more than one key:value element, I managed to make some progress, but it is still not as it should be – the following code:

JavaScript

is making result as below:

JavaScript

and as you can see it doesn’t work properly – for example there should be no blank line between first occurrence of byr and first occurrence of hgt and so on. It seemed to me that the last if statement

JavaScript

is protecting me from such situation, but now I totally don’t get why isn’t it as I “predicted”. Please help. Thanks from advance <3

Advertisement

Answer

Try this

JavaScript

Having this in file.txt :

JavaScript

Running the above script gives me this in output.txt:

JavaScript

Hope this is what you needed ?

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement