Jump to content


Photo

[SOLVED]Sort array by popularity


  • Please log in to reply
5 replies to this topic

#1 ego0

ego0
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 13 June 2006 - 02:36 PM

I've hit a brick wall with the a script i'm making.

Say I have an array with the values ball,doll,cat,cat,dog,cat,ball,cat.

Is there a way to sort the array in order of the most common value inside the array.

I know that array_count_values can count the number of a certain value in an array.
So mayby I could run a loop to go through the array and count how many of each value are the array then put it into something with the value and the number of recurrences of the value then sort them that way?

Is there a much quicker way? Any help appreciated

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 13 June 2006 - 02:42 PM

That's probably the best way to go about it. There are no sort functions that will give you what you want.

Info: PHP Manual


#3 ego0

ego0
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 13 June 2006 - 02:45 PM

Thanks, I'll try and go ahead with this method. *sends email to developers demanding a function to do this*

#4 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 13 June 2006 - 02:46 PM

Post something up when you have something and we can help you.

Info: PHP Manual


#5 ego0

ego0
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 14 June 2006 - 06:13 PM

Ok, It worked BUT the array now contains values like:

15,cat
2,dog
10,donkey

But when sorted using 'rsort' it is sorted by the first number rather than both number like '15'. The previous would sort like this:-

2,dog
15,cat
10,donkey

How can you sort an array numerically by both digits?


#6 ego0

ego0
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 14 June 2006 - 06:25 PM

SOLVED used sort($array, sort_numeric);




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users