SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Another sort array question

    Searched but could not find anything I thought was useful to help.

    I have an array of file names. Those names start with either a number (3 digit) or alphabetic characters. Now, I want the alpha's first with the numeric last. I can't figure it out.

    Thanks
    E

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    What have you tried so far?
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There are many ways of doing it. The quickest is probably just sorting the array normally and then repeat checking the first item for an integer starting character, removing it and adding it to the end.

    Something like:
    PHP Code:
    function AlphaFirst($Array){
        if(
    count($Array) > 0){
            
    sort($Array);
            while(
    is_numeric(substr($Array[0], 01))){
                
    $Array[] = array_shift($Array);
            }
        }
        return 
    $Array;
    }

    $ToSort = array('fileA''1fileA''4fileA''fileB');
    $Sorted AlphaFirst($ToSort);
    var_dump($ToSort$Sorted); 
    It might be more efficient to use one of the many sorting capabilities that PHP has to offer, but I don't think it would be. For reference, check usort out.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    asort, natsort, uksort. All have the numbers first and alpha last. I want alpha first. Oh, and I put the leading character of the alpha as an underscore (does put the sort at the beginning of the alphas but...)

    P.S. Data is like
    001.jpg
    005.jpg
    047.jpg
    picture1.jpg
    005.jpg
    another picture1.jpg

    etc..

  5. #5
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jake,

    I used your "example" and it works as I want it. Thanks.

    E


Bookmarks

Posting Permissions

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