0

I have an associative array of format

[Article_title_1] => Array
        (
            [description] => Trial
            [created] => date
        )
[Article_title_2] => Array
        (
            [description] => Trial
            [created] => date
        )

I want sort the array in alphabetical order(by title) where Article_title_2 is the title of the article.

I tried ksort. It does not work. I was trying to use cakephp's set sort, could not get it to work either.

I appreciate any help.

Thanks.

6
  • 1
    What do you mean by ksort not working? Commented Mar 9, 2011 at 11:20
  • Why didn't ksort work for you? Commented Mar 9, 2011 at 11:21
  • Yes, what output is ksort giving you? Commented Mar 9, 2011 at 11:22
  • What does "it does not work" mean? I can assure you that it most certainly does "work". Commented Mar 9, 2011 at 11:22
  • Show the code where you tried ksort Commented Mar 9, 2011 at 11:24

1 Answer 1

5

ksort() will treat each of your keys as a string, so it will sort according to normal string ordering rules, and is case-sensitive. This means you could end up with:

Article_Title_12
Article_title_1
Article_title_10
Article_title_11
Article_title_2
Article_title_3
Article_title_4
Article_title_5
Article_title_6
Article_title_7
Article_title_8
Article_title_9

If this is your problem, then you will need to use uksort() with a custom comparison function

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.