LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2009, 01:52 PM   #1
RavenLX
Member
 
Registered: Oct 2004
Posts: 98

Rep: Reputation: 15
Question php sort help - sort numerical descending then by alphabetical?


Using PHP 5.x.

I would like to sort a set of values first numerically, then alphabetically. For example, here's my code so far:

Code:
<?php

	print "<p style=\"font-family:verdana;font-size:10pt\">\n";
	$myArray = array("1223:starfruit", "34112:oranges", "1223:zucchini", "321:apples", "34112:pears", "1223:tomatoes");

	rsort($myArray, SORT_NUMERIC);

	for($i = 0; $i <= count($myArray); $i++) {
		print "$myArray[$i]<br />\n";
	}
	print "</p>\n";
?>
The output is this:

Code:
34112:pears
34112:oranges
1223:tomatoes
1223:zucchini
1223:starfruit
321:apples
I would like the output to be this instead:

Code:
34112:oranges
34112:pears
1223:starfruit
1223:tomatoes
1223:zucchini
321:apples
I have fiddled with array_multisort but I can't seem to get my head around it or the many sort routines in PHP. Help would be appreciated.
 
Old 03-10-2009, 10:41 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You want to look at usort, where it sorts using a user defined function, you will then need to write your own function that will compare the values, first comparing the number and if they are equal then compare the characters.
 
Old 03-11-2009, 06:42 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
I knew this thread sounded familiar...
 
Old 03-11-2009, 08:35 AM   #4
RavenLX
Member
 
Registered: Oct 2004
Posts: 98

Original Poster
Rep: Reputation: 15
Thank you both. Yes, that was my thread. The thing is, the perl functions I ended up writing to do it (there were TWO of them that had to work together) might not work in PHP. I have to start from scratch again, it looks like. That takes time and I'm to finish this whole project this week. If I have to rewrite the functions I won't be able to finish in time.

So what I'll do is not have it sort the items alphabetically after values are sorted and get the rest of the project finished, and then just go back and try to work more on the alphabetical thing later. Perl and PHP are very similar (I was easily able to port another function into PHP from perl that I needed) but not everything is similar. Some things I have to do over.

I was hoping that PHP already *had* a function that would take care of it without me resorting to having to re-invent the wheel all over again.
 
  


Reply

Tags
php, sort



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 10:12 AM
selection sort compiles but does not sort the array as desired ganesha Programming 2 04-20-2008 07:44 AM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 08:40 PM
how to 'sort' by file extension then alphabetical order adamrosspayne Linux - Newbie 4 07-04-2006 10:53 PM
Alphabetical sort wierdness SuperNull Linux - General 2 11-18-2005 12:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration