Req #62840 [Com]: Add sort flag to ArrayObject::ksort

2012-08-20 Thread reeze dot xia at gmail dot com
Edit report at https://bugs.php.net/bug.php?id=62840edit=1

 ID: 62840
 Comment by: reeze dot xia at gmail dot com
 Reported by:bert at becoded dot be
 Summary:Add sort flag to ArrayObject::ksort
 Status: Open
 Type:   Feature/Change Request
 Package:SPL related
 PHP Version:5.4.5
 Block user comment: N
 Private report: N

 New Comment:

Sounds reasonable to me I am working on a patch for this request.
Will send a pull request later.

Thanks


Previous Comments:

[2012-08-16 15:23:36] bert at becoded dot be

Description:

When mixing numeric and strings for the keys, you can get unexpected results. 
With the ksort, you can specify a sort flag (see the flags on 
http://www.php.net/manual/en/function.sort.php). Using that, you are able to 
change the sort behaviour.
It would be great if we could specify the same sort flag for ArrayObject::ksort.
So instead of public void ArrayObject::ksort ( void ) it would be public void 
ArrayObject::ksort ( [ int $sort_flags = SORT_REGULAR ])

Test script:
---
?php
$list = array('key_3' = 'Value 3', 'Unknown key 1', 'key_1' = 'Value 1', 
'key_2' = 'Value 2', 'key_0' = 'Value 0', 'Unknown key 2');
ksort($list);
var_dump($list);
ksort($list, SORT_STRING);
var_dump($list);

$list = new ArrayObject();
$list-offsetSet('key_3', 'Value 3');
$list-append('Unknown key 1');
$list-offsetSet('key_1', 'Value 1');
$list-offsetSet('key_2', 'Value 2');
$list-offsetSet('key_0', 'Value 0');
$list-append('Unknown key 2');
$list-ksort();
//$list-ksort(SORT_STRING);
var_dump($list);

Expected result:

If $list-ksort(SORT_STRING) would work, then I would expect
object(ArrayObject)[1689]
  string 'Unknown key 1' (length=13)
  string 'Unknown key 2' (length=13)
  public 'key_0' = string 'Value 0' (length=7)
  public 'key_1' = string 'Value 1' (length=7)
  public 'key_2' = string 'Value 2' (length=7)
  public 'key_3' = string 'Value 3' (length=7)



Actual result:
--
//ksort
array (size=6)
  'key_0' = string 'Value 0' (length=7)
  'key_1' = string 'Value 1' (length=7)
  0 = string 'Unknown key 1' (length=13)
  'key_2' = string 'Value 2' (length=7)
  'key_3' = string 'Value 3' (length=7)
  1 = string 'Unknown key 2' (length=13)

//ksort with sort flag SORT_STRING
array (size=6)
  0 = string 'Unknown key 1' (length=13)
  1 = string 'Unknown key 2' (length=13)
  'key_0' = string 'Value 0' (length=7)
  'key_1' = string 'Value 1' (length=7)
  'key_2' = string 'Value 2' (length=7)
  'key_3' = string 'Value 3' (length=7)

//ArrayObject::ksort
object(ArrayObject)[1689]
  public 'key_0' = string 'Value 0' (length=7)
  public 'key_1' = string 'Value 1' (length=7)
  string 'Unknown key 1' (length=13)
  public 'key_2' = string 'Value 2' (length=7)
  public 'key_3' = string 'Value 3' (length=7)
  string 'Unknown key 2' (length=13)






-- 
Edit this bug report at https://bugs.php.net/bug.php?id=62840edit=1


Req #62840 [Com]: Add sort flag to ArrayObject::ksort

2012-08-20 Thread reeze dot xia at gmail dot com
Edit report at https://bugs.php.net/bug.php?id=62840edit=1

 ID: 62840
 Comment by: reeze dot xia at gmail dot com
 Reported by:bert at becoded dot be
 Summary:Add sort flag to ArrayObject::ksort
 Status: Closed
 Type:   Feature/Change Request
 Package:SPL related
 PHP Version:5.4.5
 Assigned To:laruence
 Block user comment: N
 Private report: N

 New Comment:

cool, I implemented it last night, but missing a better test case.
I think the error message isn't correct, so I didn't sent it.

you could see that, the warning message is not really exactly correct.
It just a proxy to call asort($obj, $flag) if the $flag is not valid.
It complained about the 2 parameter is not valid but not the first parameter.

$obj-sort('adfsd');  // 1
sort($ar, 'adfds');   // 2


Previous Comments:

[2012-08-21 05:30:45] larue...@php.net

This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.




[2012-08-21 05:30:07] larue...@php.net

hmm, after a quick look, this will be very easy to implemented. then I did it. 
:)


[2012-08-21 00:59:20] reeze dot xia at gmail dot com

Sounds reasonable to me I am working on a patch for this request.
Will send a pull request later.

Thanks


[2012-08-16 15:23:36] bert at becoded dot be

Description:

When mixing numeric and strings for the keys, you can get unexpected results. 
With the ksort, you can specify a sort flag (see the flags on 
http://www.php.net/manual/en/function.sort.php). Using that, you are able to 
change the sort behaviour.
It would be great if we could specify the same sort flag for ArrayObject::ksort.
So instead of public void ArrayObject::ksort ( void ) it would be public void 
ArrayObject::ksort ( [ int $sort_flags = SORT_REGULAR ])

Test script:
---
?php
$list = array('key_3' = 'Value 3', 'Unknown key 1', 'key_1' = 'Value 1', 
'key_2' = 'Value 2', 'key_0' = 'Value 0', 'Unknown key 2');
ksort($list);
var_dump($list);
ksort($list, SORT_STRING);
var_dump($list);

$list = new ArrayObject();
$list-offsetSet('key_3', 'Value 3');
$list-append('Unknown key 1');
$list-offsetSet('key_1', 'Value 1');
$list-offsetSet('key_2', 'Value 2');
$list-offsetSet('key_0', 'Value 0');
$list-append('Unknown key 2');
$list-ksort();
//$list-ksort(SORT_STRING);
var_dump($list);

Expected result:

If $list-ksort(SORT_STRING) would work, then I would expect
object(ArrayObject)[1689]
  string 'Unknown key 1' (length=13)
  string 'Unknown key 2' (length=13)
  public 'key_0' = string 'Value 0' (length=7)
  public 'key_1' = string 'Value 1' (length=7)
  public 'key_2' = string 'Value 2' (length=7)
  public 'key_3' = string 'Value 3' (length=7)



Actual result:
--
//ksort
array (size=6)
  'key_0' = string 'Value 0' (length=7)
  'key_1' = string 'Value 1' (length=7)
  0 = string 'Unknown key 1' (length=13)
  'key_2' = string 'Value 2' (length=7)
  'key_3' = string 'Value 3' (length=7)
  1 = string 'Unknown key 2' (length=13)

//ksort with sort flag SORT_STRING
array (size=6)
  0 = string 'Unknown key 1' (length=13)
  1 = string 'Unknown key 2' (length=13)
  'key_0' = string 'Value 0' (length=7)
  'key_1' = string 'Value 1' (length=7)
  'key_2' = string 'Value 2' (length=7)
  'key_3' = string 'Value 3' (length=7)

//ArrayObject::ksort
object(ArrayObject)[1689]
  public 'key_0' = string 'Value 0' (length=7)
  public 'key_1' = string 'Value 1' (length=7)
  string 'Unknown key 1' (length=13)
  public 'key_2' = string 'Value 2' (length=7)
  public 'key_3' = string 'Value 3' (length=7)
  string 'Unknown key 2' (length=13)






-- 
Edit this bug report at https://bugs.php.net/bug.php?id=62840edit=1