Web Analytics Made Easy -
StatCounter sorting - CodingForum

Announcement

Collapse
No announcement yet.

sorting

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • sorting

    hi - i've got a comma delimited text file with hundreds of lines. the first field (the first bit before the first comma of each line) indicates an item id number of 4 digits. the id's aren't sequential. i'll call this 'masterlist' for the example.

    e.g.

    1845,california,vallejo,91204,510
    3983,california,los angeles,93033,805
    9088,michigan,detroit,49015,712
    3526,nevada,las vegas,87455,702

    this'll be exploded by line breaks, then by commas, and a table will be output with a basic ui. we have several users that on login will open another text file appropriate to that user with an ordered list of id's. this is a ranking list determined earlier by that user.

    e.g, user bill logs in, and is returned the list he made up earlier that week:
    3983,9088,3526,1845

    what i dont' know how to do is sort by the second elements in the second array.

    assuming i exploded that list by commas as well, i'd have

    ranking = [3983,9088,3526,1845];

    and

    items = [[1845,california,vallejo,91204,510],[3983,california,los angeles,93033,805],[9088,michigan,detroit,49015,712],[3526,nevada,las vegas,87455,702]];

    that ill need to ultimate output as

    3983,california,los angeles,93033,805
    9088,michigan,detroit,49015,712
    3526,nevada,las vegas,87455,702
    1845,california,vallejo,91204,510

    any hints? these are large lists (about 1500 lines), and we're on a timer, and there are hundreds of users, so just looping through and matching element to element probably wont be feasible i'd imagine, but could be wrong.

  • #2
    multi array sorting

    Hey,

    You can try the usort function...

    <?php
    $testarray = array();
    $testarray["1845"] = array("1845","california","vallejo","91204","510");
    $testarray["3983"] = array("3983","california","los angeles","93033","805");
    $testarray["9088"] = array("9088","michigan","detroit","49015","712");
    $testarray["3526"] = array("3526","nevada","las vegas","87455","702");

    function sortbycity($a , $b) {
    $a1 = array_values($a);
    $b2 = array_values($b);
    $city1 = $a1[2]; //city position in array
    $city2 = $b2[2]; //city position in array

    return strcmp($city1, $city2);
    }

    usort($testarray, "sortbycity");

    echo "<pre>"; echo print_r($testarray); echo "</pre>";
    ?>

    The php website also has a few examples if you search for sort.
    (i.e. : http://www.php.net/manual/en/function.sort.php#51366)
    Hope this helps!


    kent

    Comment

    Working...
    X