Is it really that hard to sort php arrays alphabetically?

Discussion in 'PHP' started by offthedome, Jan 3, 2012.

  1. #1
    I have a cgi script that sorts files alphabetically, so that, for example, "Best" goes before "BFF" and "102" goes before "11". For SEO purposes, I can no longer sort the analogous php array in anything other than alphabetical order. I need a php function that will sort a php array completely alphabetically.

    Is there seriously no automated way to do that? Do I have to go and figure out how to write a function that will sort an array alphabetically? Does anyone have such a method?
     
    offthedome, Jan 3, 2012 IP
  2. DevSwift

    DevSwift Peon

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Use the asort() function
     
    DevSwift, Jan 3, 2012 IP
  3. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #3
    From what I understand, asort() is case-sensitive. Is that not correct?
     
    offthedome, Jan 3, 2012 IP
  4. BRWebConsultingLtd

    BRWebConsultingLtd Member

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    28
    #4
    uksort();


    To use without case-sensitivity, use like this:


    uksort($your_array, "strnatcasecmp");
    Code (markup):

    Another is natcasesort();


    Also, for future reference, take a look at this: www . php . net/manual/en/array.sorting.php
    Hope that helps :)
     
    BRWebConsultingLtd, Jan 4, 2012 IP
  5. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #5
    I'll try the uksort() example you did. I can't find thorough descriptions of "strnatcasecmp". natcasesort() does not sort alphabetically. It sorts the same style as Microsoft Windows, so that for example "11" goes before "101" even though the reverse is true when ordered alphabetically.
     
    offthedome, Jan 4, 2012 IP
  6. BRWebConsultingLtd

    BRWebConsultingLtd Member

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    28
    #6
    BRWebConsultingLtd, Jan 4, 2012 IP
  7. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #7
    Neither option worked.

    The uksort() example had no effect on the ordering of the array. natcasesort() had the expected effect of sorting letters alphabetically but sorting numbers by actual number value, so 11 went before 101.

    I have to ask again, is it really this hard to sort alphabetically with something as ubiquitous as php?
     
    offthedome, Jan 4, 2012 IP
  8. BRWebConsultingLtd

    BRWebConsultingLtd Member

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    28
    #8
    Can I see the array you're using?
     
    BRWebConsultingLtd, Jan 5, 2012 IP
  9. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #9
    
    <html><body>
    <?php 
    
    $a = array("1","11","01","101","10","best", "BFF", "Bets");
    echo "plain array: ";print_r($a);
    $a = array("1","11","01","101","10","best", "BFF", "Bets");
    sort($a);
    echo "sort: ";print_r($a);
    $a = array("1","11","01","101","10","best", "BFF", "Bets");
    asort($a);
    echo "asort: "; print_r($a);
    $a = array("1","11","01","101","10","best", "BFF", "Bets");
    natcasesort($a);
    echo "natcasesort: "; print_r($a);
    $a = array("1","11","01","101","10","best", "BFF", "Bets");
    uksort($a, "strnatcasecmp");
    echo "uksort with strnatcasecmp: "; print_r($a);
     ?>
    
    </body></html>
    
    PHP:
     
    offthedome, Jan 5, 2012 IP
  10. BRWebConsultingLtd

    BRWebConsultingLtd Member

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    28
    #10
    So, natcasesort() is nearly there, I don't get what the problem with the numbers is, it's sorting them from lowest to highest. How do you need them sorted, as numbers aren't alphabetical...
     
    BRWebConsultingLtd, Jan 5, 2012 IP
  11. offthedome

    offthedome Active Member

    Messages:
    446
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    95
    #11
    I guess the correct term instead of "alphabetical" would be (case-insensitive) "lexicographical", sometimes called "dictionary order", but any ordered set can be appended to the alphabet to create an ordering.

    Dictionary ordering is the way perl searches for files in a directory, and it's the way javascript autmatically sorts arrays. The problem is that I can't go back and sort any other way because I've sorted pages by dictionary ordering forever because that's how perl created pages. I also figured something like dictionary ordering would be so useful, considering that's how analog content (books etc.) and most website content have always done it, that there would be an easy-to-implement and easy-to-find way to sort that way in PHP.
     
    offthedome, Jan 5, 2012 IP