Sorting text file content

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shiavarj rh
    New Member
    • Oct 2011
    • 1

    Sorting text file content

    Hello friends, i'm writing search page for my local website. i'm storing all file list in text file. And also adding a value to each line at the beginning. now i want to sort file content in descending order according to begin value.

    Text File having list like this:

    5|<h3><a href='http://localhost/rchiving-tagging/archiving

    2|<h3><a href='http://localhost/rchiving-tagging/archiving

    3|<h3><a href='http://localhost/rchiving-tagging/archiving

    4|<h3><a href='http://localhost/rchiving-tagging/archiving

    can anyone help it plz?
  • omerbutt
    Contributor
    • Nov 2006
    • 638

    #2
    Hi Shiavarj rh,

    create the file object

    read the contents of the file

    split them into an array (explode()) on the condition of line break i.e. \n

    sort the array(array_sor t())

    reverse the array (use array_reverse() )

    then re-join the array use implode() function (so that you can

    join them on to a condition i.e. \n)

    rewrite the file

    hope that helps,
    Omer Aslam

    Comment

    Working...