SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2005
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I sort array ignoring case and accents?

    Hello,
    I have an array having username looking like this.

    PHP Code:
     $sort_test =  {
      
    'sort_test_1' => sing
      
    'sort_test_2'    => SONG
      
    'sort_test_3'        => SANG
      
    'sort_test_4'        => sÚng

    As you see, it contains upper/lower case and accents

    I'd like to sort the array ignoring case and accents.
    So I want the array to be sorted like this

    PHP Code:
    $sort_test = {
      
    'sort_test_3'        => SANG
      
    'sort_test_4'        => sÚng
      
    'sort_test_1' => sing
      
    'sort_test_2'    => SONG

    if I tried to use natcasesort($sort_test);
    the array order is SANG, sing, SONG, sÚng and still sÚng comes last, but I'd like to consider Ú as e, so it should come after SANG
    How can I do this?

    Actaully sÚng is just one example, my application usernames contain many different accents.
    How can I ignore the accents and apply normal alphabetical sort order?

    Thanks,

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lmsook View Post
    Hello,
    I have an array having username looking like this.

    PHP Code:
     $sort_test =  {
      
    'sort_test_1' => sing
      
    'sort_test_2'    => SONG
      
    'sort_test_3'        => SANG
      
    'sort_test_4'        => sÚng

    As you see, it contains upper/lower case and accents

    I'd like to sort the array ignoring case and accents.
    So I want the array to be sorted like this

    PHP Code:
    $sort_test = {
      
    'sort_test_3'        => SANG
      
    'sort_test_4'        => sÚng
      
    'sort_test_1' => sing
      
    'sort_test_2'    => SONG

    if I tried to use natcasesort($sort_test);
    the array order is SANG, sing, SONG, sÚng and still sÚng comes last, but I'd like to consider Ú as e, so it should come after SANG
    How can I do this?

    Actaully sÚng is just one example, my application usernames contain many different accents.
    How can I ignore the accents and apply normal alphabetical sort order?

    Thanks,
    mm simple solution copy the array, iterate over the array replacing all characters with accents and then use asort which maintains the corresponding index. When you need to display iterate over the sorted array but retrieve the values from the unsorted array.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you should find an existing collation that has the character ordering rules you desire. Maybe something like

    PHP Code:
    setlocale(LC_COLLATE'English');
    usort($arr, function($a$b){
        return 
    strcoll(strtolower($a), strtolower($b));
    }); 


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
  •