Welcome to WebmasterWorld Guest from 35.173.48.224

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

how to sort a multi-dimensional array

sorting on one field

     
4:56 pm on Mar 14, 2003 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7577
votes: 4


I have while loop that builds an array of the field values and adds one additional field calculated based on the values within certain fields. So:

while () {
$rank[] = array(1 => "field1", 2 => "field2",
3 => "field3", 4 => "field4", 5 => "field5");
}

Field 5 is the calculated value (the weight) and now I want to sort the array ($rank) on this. I was looking at array_multisort to do this but I'm stuck on syntax - or is this the wrong function to use?

5:04 pm on Mar 14, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


right function but the syntax is a bit of a pain.
array_multisort($rank[4],SORT_DESC,SORT_NUMERIC,$rank[3],$rank[2],$rank[1],$rank[0]);

I think that is right. I always have problems because if you don't put the other elements in thee it doesn't keep the rows together.

try that and see if it works, if not I will go find anothwer script I have that multisorts and check it out.

5:14 pm on Mar 14, 2003 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7577
votes: 4


Hey jatar_k,
Thanks. So the column I want to sort on can't be identified by its key? You used $rank[4] when I expected it to be $rank[5].

Also, does the order in which you call the columns matter? e.g. array_multisort(5, 4, 3, 2, 1)?

5:18 pm on Mar 14, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15756
votes: 0


try it both ways, I haven't used array multisort by key so I am not sure.

the first one is the one to sort by, then the type of sort and then the rest of the fields to keep with it.

I don't think it matters what order the fields are in as long as they are there.

5:36 pm on Mar 14, 2003 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7577
votes: 4


I just realized I can't do it this way. I'm going to have to rethink how I accomplish the goal. The problem is that $rank is an array containing one array: $rank = array(field1, field2, field3)

So using multisort only sorts the array based on all fields rather than what I need which is to sort on one field.

8:03 pm on Mar 14, 2003 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7577
votes: 4


Ha - more than one way to get this done. I ended up writing it out to a temp table and then inserting the calculated value. I turned around an queried that table to get the information in the order I wanted it. Pretty quick too.
8:08 pm on Mar 14, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Dec 6, 2001
posts:99
votes: 0


That's similar to how I had to do it, except I had multi-multi-dimensional arrays (LOTS of data). ;-)

noSanity

12:01 am on Mar 15, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 14, 2002
posts:118
votes: 0


Would be nice if peopler stated the language their using - Perl & PHP can lok quite similar.

PHP has an array_multisort function. Look it up on the php.net manual.

Allen

9:21 pm on Mar 17, 2003 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lorax is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Mar 31, 2002
posts:7577
votes: 4


Thanks Allen,
Sorry about not being more clear - I was speaking of PHP.

I'm aware of the array_multisort function and php.net but as I pointed out - I couldn't use it for what I was trying to get done. Which is why I switched gears and built a temp table.

1:16 am on Mar 18, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


>>Perl [perl.com] & PHP [php.net] can look quite similar...

..but there is no array [php.net] keyword in Perl [perl.com] :). At least I do not know of it and my auto linking proxy does not as well: %array% <- no link!

Andreas

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members