Skip to content

Commit 152590c

Browse files
committed
Fix -a output order to match ls: non-hidden files first, hidden files last, with '.' and '..' at the top
1 parent 4ebe461 commit 152590c

File tree

1 file changed

+15
-0
lines changed
  • implement-shell-tools/ls

1 file changed

+15
-0
lines changed

implement-shell-tools/ls/ls.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313
for path in paths :
1414
files_list=os.listdir(path)
1515
files_list.sort(key=str.lower)
16+
17+
# Divide into hidden and non-hidden
18+
non_hidden = [f for f in files_list if not f.startswith(".")]
19+
hidden = [f for f in files_list if f.startswith(".")]
20+
21+
# Sort each list
22+
non_hidden.sort(key=str.lower)
23+
hidden.sort(key=str.lower)
24+
25+
# Add '.' and '..' at the start if -a
26+
if args.a:
27+
files_list = ['.', '..'] + non_hidden + hidden
28+
else:
29+
files_list = non_hidden
30+
1631
for item in files_list :
1732
if args.one and args.a :
1833
print(item)

0 commit comments

Comments
 (0)