[UPHPU] usort help

Wade Shearer wadeshearer.lists at me.com
Tue Jun 4 08:11:58 MDT 2013


The reason that I can't do the sort in the SQL is because I am make four queries and then merging them first. 


On Jun 4, 2013, at 8:02, Wade Shearer <wadeshearer.lists at me.com> wrote:

> Another great idea. 
> 
> For this, I actually didn't change them and change them back. I made the change in the sort condition, like this:
> 
> foreach ($all as $key => $row) {
>   $year[$key] = $row['year_due'];
>   $type[$key] = $row['type'] == 3 ? 6 : 3;
>   $item[$key] = $row['title'];
> }
> 
> array_multisort($year, SORT_DESC, $type, SORT_ASC, $item, SORT_ASC, $all);
> 
> 
> On 4 Jun 2013, at 7:59, Chris London <me at chrislondon.co> wrote:
> 
>> If you're pulling them out a MySQL database you could do this too:
>> 
>> SELECT
>>   columnA,
>>   columnB,
>>   column1243,
>>   IF(column1243 = 3, 5, column1243) as sortColumn1245
>> FROM
>>   table
>> ORDER BY
>>   sortColumn1245, columnB
>> 
>> that way it comes out of the database in the correct order and you don't
>> have to change any 5's back to 3's or anything.
>> 
>> 
>> 
>> On Tue, Jun 4, 2013 at 7:53 AM, Wade Shearer <wadeshearer.lists at me.com>wrote:
>> 
>>> Good idea, Chris. That did the trick.
>>> 
>>> 
>>> On 4 Jun 2013, at 7:41, Chris London <me at chrislondon.co> wrote:
>>> 
>>>> What if you changed all of the 3's to 5's and then sorted numerically,
>>> then
>>>> you can change the 5's back to 3's after?
>>>> 
>>>> 
>>>> On Tue, Jun 4, 2013 at 7:39 AM, Wade Shearer <wadeshearer.lists at me.com
>>>> wrote:
>>>> 
>>>>> The values in that column will only be one of those four numbers. So,
>>> I'm
>>>>> trying to sort first by that column and then by another.
>>>>> 
>>>>> So, for example, if I had this:
>>>>> 
>>>>> 3
>>>>> 4
>>>>> 1
>>>>> 2
>>>>> 2
>>>>> 1
>>>>> 1
>>>>> 3
>>>>> 
>>>>> 
>>>>> It would be sorted, like this:
>>>>> 
>>>>> 1
>>>>> 1
>>>>> 1
>>>>> 2
>>>>> 2
>>>>> 4
>>>>> 3
>>>>> 3
>>>>> 
>>>>> 
>>>>> If I need to sort by multiple columns, is usort() even going to work? I
>>>>> have been trying array_multisort() as well and have it sorting on
>>> multiple
>>>>> columns. I just can't get the custom sort order for the type column
>>>>> (1,2,4,3).
>>>>> 
>>>>> 
>>>>> On 4 Jun 2013, at 7:36, Chris London <me at chrislondon.co> wrote:
>>>>> 
>>>>>> are you trying to do a certain pattern? or just 1,2,4,3? like if there
>>>>> were
>>>>>> 5 or 10 items how would it be sorted?
>>>>>> 
>>>>>> 
>>>>>> On Tue, Jun 4, 2013 at 7:26 AM, Wade Shearer <wadeshearer.lists at me.com
>>>>>> wrote:
>>>>>> 
>>>>>>> Thanks, Chris. That only sorts numerically though, right? How do I
>>> sort
>>>>> it
>>>>>>> in this order? 1, 2, 4, 3?
>>>>>>> 
>>>>>>> 
>>>>>>> On 4 Jun 2013, at 6:40, Chris London <me at chrislondon.co> wrote:
>>>>>>> 
>>>>>>>> Hey Wade. This should help:
>>>>>>>> 
>>>>>>>> function sortByColumn($a, $b) {
>>>>>>>> $key = 'column';
>>>>>>>> $a = $a[$key];
>>>>>>>> $b = $b[$key];
>>>>>>>> 
>>>>>>>> if ($a == $b) {
>>>>>>>>    return 0;
>>>>>>>> } else {
>>>>>>>>    return ($a < $b) ? -1 : 1;
>>>>>>>> }
>>>>>>>> }
>>>>>>>> 
>>>>>>>> usort($arrayToSort, 'sortByColumn');
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> 
>>>>>>>> On Tue, Jun 4, 2013 at 12:33 AM, Wade Shearer <
>>>>> wadeshearer.lists at me.com
>>>>>>>> wrote:
>>>>>>>> 
>>>>>>>>> I am trying to sort a multidimensional array by a custom sort
>>> order. I
>>>>>>>>> sounds like it's possible with usort() but I can't get it to work.
>>>>> Each
>>>>>>>>> array within the multidimensional array has a type "column" that is
>>> a
>>>>>>>>> numeric value.
>>>>>>>>> 
>>>>>>>>> I want them to be sorted in this order: 1,2,4,3
>>>>>>>>> 
>>>>>>>>> Can anyone provide me with an example of how to do this? The place
>>>>>>> where I
>>>>>>>>> am lost is that I'm supposed to compare A with B and return -1, 0,
>>> or
>>>>>>> 1. I
>>>>>>>>> get that part, but where do I incorporate my custom sort order?
>>>>>>>>> 
>>>>>>>>> _______________________________________________
>>>>>>>>> 
>>>>>>>>> UPHPU mailing list
>>>>>>>>> UPHPU at uphpu.org
>>>>>>>>> http://uphpu.org/mailman/listinfo/uphpu
>>>>>>>>> IRC: #uphpu on irc.freenode.net
> 
> 
> _______________________________________________
> 
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net


More information about the UPHPU mailing list