Jump to content

Sorting multidimensional array by multiple fields


litebearer

Recommended Posts

The scenario:

1. flatfile database
      consists of records delimited by carriage returns.
      records have fields delimited by commas

      example of file structure and content:
[quote]
        1,Nicholas,,Stoia,6992 Biscayne,White Lake,MI,48383,1,13,1946,,,,,,,
        2,Myles,Edward,Dean,,,WV,,0,0,0,,,,,,,
        3,Sharon,Marie,Stoia,6992 Biscayne,White Lake,MI,48383,7,16,1955,,,,,,,
        4,Jonathon,Hunter,Stoia,6992 Biscayne,White Lake,MI,48383,12,13,1988,,,,,,,
        5,Rebecca,,Dean,,,WV,,0,0,0,,,,,,,
        6,Gilbert,Rial,Stebbins,22600 Bluewater Drive,Macomb Twp,MI,48044,0,0,0,,,,,,,
        7,Beverly,,Stebbins,22600 Bluewater Drive,Macomb Twp,MI,48044,0,0,0,,,,,,,
        8,Sean,Michael,Dean,167 Weston Road,Wellesley,MA,02482,0,0,0,,,,,,,
[/quote]

2. multidimensional array created as follows:

[code]
        // read file into array
        $file = file("faf.txt");
        $count = count($file);

        // convert first array to multidimensional array
        $i = 0;
        for($i=0;$i<$count;$i++) {
        $file_cards[$i] = explode(",", $file[$i]);
        }

3. sort by last name (

        function compare($x, $y){
          if ( $x[3] == $y[3] )
            return 0;
          else if ( $x[3] < $y[3] )
            return -1;
          else
            return 1;
        }

        usort($file_cards, 'compare');
[/code]

The Question:

The above works fine; however,
in addition to sorting by last name [code]$file_cards[x][3] [/code]
I would like to further sort by first name [code]$file_cards[x][1][/code]
and then by middle name [code]$file_cards[x][2] [/code].


How?

Thanks,

Lite...


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.