1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 20:47:46 +00:00

ls: string compare if version_cmp returns equal

This commit is contained in:
John Shin 2023-07-04 16:08:54 -07:00
parent d2e7ba2da1
commit 4acd02c7a1
2 changed files with 49 additions and 6 deletions

View file

@ -1931,8 +1931,10 @@ fn sort_entries(entries: &mut [PathData], config: &Config, out: &mut BufWriter<S
Sort::Size => entries.sort_by_key(|k| Reverse(k.md(out).map(|md| md.len()).unwrap_or(0))), Sort::Size => entries.sort_by_key(|k| Reverse(k.md(out).map(|md| md.len()).unwrap_or(0))),
// The default sort in GNU ls is case insensitive // The default sort in GNU ls is case insensitive
Sort::Name => entries.sort_by(|a, b| a.display_name.cmp(&b.display_name)), Sort::Name => entries.sort_by(|a, b| a.display_name.cmp(&b.display_name)),
Sort::Version => entries Sort::Version => entries.sort_by(|a, b| {
.sort_by(|a, b| version_cmp(&a.p_buf.to_string_lossy(), &b.p_buf.to_string_lossy())), version_cmp(&a.p_buf.to_string_lossy(), &b.p_buf.to_string_lossy())
.then(a.p_buf.to_string_lossy().cmp(&b.p_buf.to_string_lossy()))
}),
Sort::Extension => entries.sort_by(|a, b| { Sort::Extension => entries.sort_by(|a, b| {
a.p_buf a.p_buf
.extension() .extension()

View file

@ -2312,15 +2312,56 @@ fn test_ls_version_sort() {
let scene = TestScenario::new(util_name!()); let scene = TestScenario::new(util_name!());
let at = &scene.fixtures; let at = &scene.fixtures;
for filename in [ for filename in [
"a2", "b1", "b20", "a1.4", "b3", "b11", "b20b", "b20a", "a100", "a1.13", "aa", "a1", "aaa", "a2",
"abab", "ab", "a01.40", "a001.001", "b1",
"b20",
"a1.4",
"a1.40",
"b3",
"b11",
"b20b",
"b20a",
"a100",
"a1.13",
"aa",
"a1",
"aaa",
"a1.00000040",
"abab",
"ab",
"a01.40",
"a001.001",
"a01.0000001",
"a01.001",
"a001.01",
] { ] {
at.touch(filename); at.touch(filename);
} }
let mut expected = vec![ let mut expected = vec![
"a1", "a001.001", "a1.4", "a1.13", "a01.40", "a2", "a100", "aa", "aaa", "ab", "abab", "b1", "a1",
"b3", "b11", "b20", "b20a", "b20b", "", // because of '\n' at the end of the output "a001.001",
"a001.01",
"a01.0000001",
"a01.001",
"a1.4",
"a1.13",
"a01.40",
"a1.00000040",
"a1.40",
"a2",
"a100",
"aa",
"aaa",
"ab",
"abab",
"b1",
"b3",
"b11",
"b20",
"b20a",
"b20b",
"", // because of '\n' at the end of the output
]; ];
let result = scene.ucmd().arg("-1v").succeeds(); let result = scene.ucmd().arg("-1v").succeeds();