PHP Questions Home | Register | Login
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website
Unanswered

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

PHP Questions Forums : PHP Questions and Answers

Jump to Last Post

Custom sorting arrays in PHP

By: Sallyjones [21-February-10 9:38AM]
12 posts
picture

Hi everyone

I hope you can help.

I need to do something that I think is quite simple but I'm having problems doing it.

What I want to do is use a sort function to sort data on length, but I can't find a function to do it, is there one?

Basically I have a list of words and I want to sort them with the longest word first and the shortest word last. Is there a function to do this?

If there's not like I think than how can I do this with custom sort function code?

Thanks

Re : Custom sorting arrays in PHP


Timjohns [26-February-10 8:41AM]
25 posts
picture

There are a range of user sorting functions which you can read more about on the php.net site.

The most usual one is the usort function: you pass it the custom function that you want to apply to sort the values, and then the name of the array that you are sorting.

So the key, then, is to write the sorting algorithm: there are plenty examples of how to write custom sorting functions out there.

Previous: Turning An Array Into A String
Next: Turning A String Of Text Into An Array



Custom Search