Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts

    Sorting Multidimensional array

    The scenario:

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

    example of file structure and content:
    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,,,,,,,
    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');
    The Question:

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


    How?

    Thanks,

    Lite...

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The only way I've found to do this is first sort by the middle name first, then append a sequential number onto the middle name, then sort by the first name+middle name, append a sequential number onto that as well, then sort by a compound value of all three sortkeys-- "lastname.firstname.middlename". So the following:

    Nielson, Olivia Sue
    Smith, Mike John
    Smith, Mike Doug
    Nielson, Tom Duke
    Smith, Mike Melvin
    Smith, Pete Bernard
    Nielson, Tom Applehead
    Smith, Adam Paul

    Would first look like

    Nielson, Tom Applehead100000
    Smith, Pete Bernard100001
    Smith, Mike Doug100002
    Nielson, Tom Duke100003
    Smith, Mike John100004
    Smith, Mike Melvin100005
    Smith, Adam Paul100006
    Nielson, Olivia Sue100007

    And then like

    Smith, Adam100000 Paul100006
    Smith, Mike100001 Doug100002
    Smith, Mike100002 John100004
    Smith, Mike100003 Melvin100005
    Nielson, Olivia100004 Sue100007
    Smith, Pete100005 Bernard100001
    Nielson, Tom100006 Applehead100000
    Nielson, Tom100007 Duke100003


    And finally like

    Nielson, Olivia100004 Sue100007
    Nielson, Tom100006 Applehead100000
    Nielson, Tom100007 Duke100003
    Smith, Adam100000 Paul100006
    Smith, Mike100001 Doug100002
    Smith, Mike100002 John100004
    Smith, Mike100003 Melvin100005
    Smith, Pete100005 Bernard100001

    The algorithm I've used in the past uses a temporary variable that I do all that appending with so I don't have to strip it all off later. I can dig it out if you want to see it.

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    298
    Thanks
    0
    Thanked 23 Times in 23 Posts
    Thanks. I'll give it a try.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •