forked from extern/nushell
fix case insensitive sort (#4449)
* fix case insensitive search * fixed test * tweak
This commit is contained in:
parent
560be6e73e
commit
6fc082f6e9
@ -138,11 +138,6 @@ pub fn sort(
|
|||||||
insensitive: bool,
|
insensitive: bool,
|
||||||
config: &Config,
|
config: &Config,
|
||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let should_sort_case_insensitively = insensitive
|
|
||||||
&& vec
|
|
||||||
.iter()
|
|
||||||
.all(|x| matches!(x.get_type(), nu_protocol::Type::String));
|
|
||||||
|
|
||||||
match &vec[0] {
|
match &vec[0] {
|
||||||
Value::Record {
|
Value::Record {
|
||||||
cols,
|
cols,
|
||||||
@ -158,6 +153,26 @@ pub fn sort(
|
|||||||
return Err(ShellError::CantFindColumn(call.head, call.head));
|
return Err(ShellError::CantFindColumn(call.head, call.head));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check to make sure each value in each column in the record
|
||||||
|
// that we asked for is a string. So, first collect all the columns
|
||||||
|
// that we asked for into vals, then later make sure they're all
|
||||||
|
// strings.
|
||||||
|
let mut vals = vec![];
|
||||||
|
for item in vec.iter() {
|
||||||
|
for col in &columns {
|
||||||
|
let val = match item.get_data_by_key(col) {
|
||||||
|
Some(v) => v,
|
||||||
|
None => Value::nothing(Span::test_data()),
|
||||||
|
};
|
||||||
|
vals.push(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let should_sort_case_insensitively = insensitive
|
||||||
|
&& vals
|
||||||
|
.iter()
|
||||||
|
.all(|x| matches!(x.get_type(), nu_protocol::Type::String));
|
||||||
|
|
||||||
vec.sort_by(|a, b| {
|
vec.sort_by(|a, b| {
|
||||||
process(
|
process(
|
||||||
a,
|
a,
|
||||||
@ -173,7 +188,7 @@ pub fn sort(
|
|||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
vec.sort_by(|a, b| {
|
vec.sort_by(|a, b| {
|
||||||
if should_sort_case_insensitively {
|
if insensitive {
|
||||||
let lowercase_left = Value::string(
|
let lowercase_left = Value::string(
|
||||||
a.into_string("", config).to_ascii_lowercase(),
|
a.into_string("", config).to_ascii_lowercase(),
|
||||||
Span::test_data(),
|
Span::test_data(),
|
||||||
|
@ -113,7 +113,7 @@ fn ls_sort_by_name_insensitive() {
|
|||||||
"#
|
"#
|
||||||
));
|
));
|
||||||
|
|
||||||
let json_output = r#"[{"name": "B.txt"},{"name": "C"},{"name": "a.txt"}]"#;
|
let json_output = r#"[{"name": "a.txt"},{"name": "B.txt"},{"name": "C"}]"#;
|
||||||
assert_eq!(actual.out, json_output);
|
assert_eq!(actual.out, json_output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user