Skip to content

[intl] Weird numeric sort in Collator #18566

Closed

Description

@arokettu

Description

The following code:

<?php

$arr = [
    '2023-02-04 14:00:00',
    '2023-01-08 12:00:00',
    '2023-01-03 12:00:00',
    '2023-01-03 12:00:00',
    '2021-01-03 12:00:00',
    '2023-01-05 14:00:00',
    '2024-01-03 12:00:00',
    '2023-01-03 12:00:00',
];

$coll = Collator::create('de');

$coll->asort($arr, Collator::SORT_REGULAR);

echo json_encode($arr, JSON_PRETTY_PRINT);

Resulted in this output:

{
    "4": "2021-01-03 12:00:00",
    "0": "2023-02-04 14:00:00",
    "1": "2023-01-08 12:00:00",
    "2": "2023-01-03 12:00:00",
    "3": "2023-01-03 12:00:00",
    "5": "2023-01-05 14:00:00",
    "7": "2023-01-03 12:00:00",
    "6": "2024-01-03 12:00:00"
}%

But I expected this output instead:

{
    "4": "2021-01-03 12:00:00",
    "2": "2023-01-03 12:00:00",
    "3": "2023-01-03 12:00:00",
    "7": "2023-01-03 12:00:00",
    "5": "2023-01-05 14:00:00",
    "1": "2023-01-08 12:00:00",
    "0": "2023-02-04 14:00:00",
    "6": "2024-01-03 12:00:00"
}

PHP Version

Since the dawn of time up to 8.4.7 apparently introduced in PHP6(!)

https://3v4l.org/fVcRj

Operating System

any

Activity

  1. arokettu commented on May 15, 2025

    @arokettu
    ContributorAuthor

    The ultimate cause seems to be collator_convert_string_to_number_if_possible thinking that it's possible to convert to number and making them all (int)2021, (int)2023, (int)2024

    if( is_numeric == IS_LONG ) {

    uint8_t collator_is_numeric( UChar *str, int32_t length, zend_long *lval, double *dval, bool allow_errors )

  2. changed the title [-]Weird numeric sort in collator[/-] [+][intl] Weird numeric sort in Collator[/+] on May 15, 2025
  3. self-assigned this
    on May 24, 2025
  4. added a commit that references this issue on May 24, 2025
    5187ff2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions