0

I'm trying to write a PHP script which will handle sorting of a CSV file by one or multiple columns and outputting the result to another file.

Is there a way to sort the CSV file without loading it entirely into memory?

2
  • How can you sort without reading the data first? Commented Oct 21, 2018 at 20:07
  • I was referring to reading rows one by one. So you would read the data but not everything at once. Commented Oct 22, 2018 at 11:05

1 Answer 1

1

No there is no reasonable way. You need the data in memory to compare and write in a file.

You could try a bubblesort if you know the length of each line. Read one line of origin and last line of a new "ordered" file. Compare them and append or prepend in new file. After this iteration do again with the new file as origin until it is sorted.

You should use a database like MySQL.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your suggestion.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.