-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ordering of ls -t, which was backwards #1057
Conversation
Now -S is wrong? |
Oops; yes, I hadn't noticed that this code handles multiple kinds of sort. |
It should be correct now. |
src/ls/ls.rs
Outdated
@@ -196,6 +196,7 @@ fn sort_entries(entries: &mut Vec<PathBuf>, options: &getopts::Matches) { | |||
.unwrap_or(std::time::UNIX_EPOCH) | |||
}); | |||
} | |||
entries.reverse(); // Newest first |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps it makes more sense to change sort_by_key
above into:
entries.sort_by(|a, b| {
get_mod_time(b, options).cmp(get_mod_time(a, options)))
});
where get_mod_time()
is defined as:
fn get_mod_time(path: &PathBuf, options: &getopts::Matches) -> time::SystemTime {
get_metadata(path, options)
.and_then(|md| md.modified())
.unwrap_or(std::time::UNIX_EPOCH)
}
That way we don't have to reverse the vector. Of course, you would also have to define a similar get_create_time
method or something to that effect for Unix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point; it seems the standard library has a neat struct for reversing sorts, so I used that.
@@ -191,9 +192,10 @@ fn sort_entries(entries: &mut Vec<PathBuf>, options: &getopts::Matches) { | |||
entries.sort_by_key(|k| get_metadata(k, options).map(|md| md.ctime()).unwrap_or(0)); | |||
} else { | |||
entries.sort_by_key(|k| { | |||
get_metadata(k, options) | |||
// Newest first | |||
Reverse(get_metadata(k, options) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't even know this was a thing lmao
@@ -191,9 +192,10 @@ fn sort_entries(entries: &mut Vec<PathBuf>, options: &getopts::Matches) { | |||
entries.sort_by_key(|k| get_metadata(k, options).map(|md| md.ctime()).unwrap_or(0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait, I may have merged this too quickly. Does this need to be reversed as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, I suppose it does.
ls: fix line that was forgotten in #1057
No description provided.