Skip to content

Incorrect sorting of negative numeric values in Parameters and User Attributes #1207

@MiracleInvoker

Description

@MiracleInvoker

Description

When Parameters (params) or User Attributes (user_attrs) contain numeric values, they are serialized as strings by the backend. The frontend TrialTable currently relies on a default sorting mechanism (likely "natural" or alphanumeric sorting).

While this mechanism correctly handles positive integers (e.g., sorting 2 before 10), it fails for negative numbers. The sorting logic appears to treat the minus sign as a character and sorts based on the magnitude of the number following it.

Current Behavior:

  • Positive numbers sort correctly: 2, 10, 100
  • Negative numbers sort incorrectly (inverted order): -1, -2, -10 (instead of -10, -2, -1).

Expected Behavior:
The dashboard should detect if the column (Parameter or User Attribute) contains numeric values and apply a true numeric sort function that correctly accounts for negative signs.

How to Reproduce

  1. Create a Python script (reproduce_issue.py) to generate a study with negative values in both parameters and user attributes:
import optuna

def objective(trial):
    # 1. Create a parameter with a negative range
    x = trial.suggest_int("x_param", -10, 10)

    # 2. Set the same value as a user attribute
    trial.set_user_attr("x_attr", x)

    return x ** 2

if __name__ == "__main__":
    study = optuna.create_study(storage="sqlite:///db.sqlite3", study_name="sort_bug_test")
    study.optimize(objective, n_trials=20)
  1. Run the script: python reproduce_issue.py
  2. Launch the dashboard: optuna-dashboard sqlite:///db.sqlite3
  3. Open the Trials tab (Trial Table).
  4. Click the header for Param x_param and UserAttribute x_attr to sort them.
  5. Observe that for both columns, negative numbers are sorted by magnitude.

Python version

Python 3.12.12

Optuna version

4.4.0

optuna-dashboard version or git revision

0.20.0

Web browser

Microsoft Edge Beta

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions