tabular_data_preview: Sort filter options natually (#64764)

## Problem

Column filter dropdown options in `tabular_data_preview` were sorted
with standard lexicographical comparison (`a.content.cmp(&b.content)`).
This led to unnatural ordering for numbers and mixed alphanumeric values
(e.g., `"item 10"` sorting before `"item 2"`).

## Solution

Switched to `util::paths::natural_sort` based comparison:
- Compares numeric segments by numerical value rather than
lexicographically (e.g., `1`, `2`, `10` instead of `1`, `10`, `2`).
- Performs case-insensitive comparison with case-sensitive tie-breaking.
- Keeps `None`/null values sorted at the bottom of the filter list.

## Testing

- Added unit tests in `table_header.rs` covering numeric runs, case
insensitivity, and null value placement.
- `cargo test -p tabular_data_preview`
- `./script/clippy -p tabular_data_preview` clean.

## Showcase

| Before | After |
| --- | --- |
| <img width="578" height="617" alt="image"
src="https://github.com/user-attachments/assets/4d74a8d0-6364-4ef6-b659-d36350e02ea0"
/> | <img width="621" height="615" alt="image"
src="https://github.com/user-attachments/assets/dadd851a-5a7a-468d-803d-3e467104ae06"
/> |

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [ ] Unsafe blocks (if any) have justifying comments
- [x] UI standards followed
- [x] Tests cover the new/changed behavior
- [x] Performance impact considered, acceptable

Release Notes:

- Improved column filter dropdown in tabular data preview to sort values
naturally (e.g., `2` before `10`).

---------

Co-authored-by: MrSubidubi <finn@zed.dev>
This commit is contained in:
Oleksandr Kholiavko
2026-10-05 16:57:01 +00:00
committed by GitHub
parent 0c70487f83
commit 8eb2fd211f
2 changed files with 23 additions and 3 deletions
+1 -1
View File
@@ -22,12 +22,12 @@ serde.workspace = true
serde_json.workspace = true
schemars.workspace = true
text.workspace = true
util.workspace = true
[dev-dependencies]
gpui = { workspace = true, features = ["test-support"] }
project = { workspace = true, features = ["test-support"] }
proptest.workspace = true
util.workspace = true
workspace = { workspace = true, features = ["test-support"] }
[features]
@@ -1,5 +1,6 @@
use std::{
borrow::Cow,
cmp,
collections::HashMap,
sync::{
Arc,
@@ -28,6 +29,25 @@ use crate::{
types::AnyColumn,
};
#[derive(PartialEq, Eq)]
struct NaturallyOrdered<'a>(&'a str);
impl Ord for NaturallyOrdered<'_> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
util::paths::natural_sort(self.0, other.0)
}
}
impl PartialOrd for NaturallyOrdered<'_> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
fn natural_content(entry: &FilterEntry) -> Option<NaturallyOrdered<'_>> {
entry.content.as_deref().map(NaturallyOrdered)
}
struct ColumnFilterRow {
entry: FilterEntry,
is_applied: bool,
@@ -90,14 +110,14 @@ impl ColumnFilterDelegate {
FilterSortOrder::AlphaThenCount => available.sort_by(|(a, a_app), (b, b_app)| {
b_app
.cmp(a_app)
.then_with(|| a.content.cmp(&b.content))
.then_with(|| natural_content(a).cmp(&natural_content(b)))
.then_with(|| b.occurred_times().cmp(&a.occurred_times()))
}),
FilterSortOrder::CountThenAlpha => available.sort_by(|(a, a_app), (b, b_app)| {
b_app
.cmp(a_app)
.then_with(|| b.occurred_times().cmp(&a.occurred_times()))
.then_with(|| a.content.cmp(&b.content))
.then_with(|| natural_content(a).cmp(&natural_content(b)))
}),
}