Read and extract a directory listing and sort them

hello : EXEMPLE

The program reads the current directory, identifies subdirectories, and sorts their names into a list using a custom comparison function (cmpByData). The sorted list is then printed in order. Here’s a summary of the main components:

Custom Structures and Allocator:
    Data: A structure to store string slices ([] const u8).
    myallocator: Uses std.heap.page_allocator.

Utility Functions:
    cmpByData: Compares two slices lexicographically.
    swapData: Swaps two items in an ArrayList.

Main Function:
    Reads the current working directory using std.fs.cwd.
    Iterates through directory entries to identify subdirectories.
    Appends subdirectory names to list_Query using a dynamic ArrayList.
    Implements a sorting mechanism by repeatedly comparing and swapping items in list_Query, appending sorted items to list_Work.
    Outputs the sorted directory names.
const std = @import("std");
pub const myallocator = std.heap.page_allocator;


const Data = struct {
  dta : [] const u8 ,
};


fn cmpByData( a: [] const u8, b: [] const u8) bool {
   const order = std.mem.order(u8, a, b);
    switch (order) {
        .lt => return false,
        else => return true,
    }
}

pub fn swapData(table :std.ArrayList(Data), r: usize, i: usize) void {
            

            const old_item = table.items[r];
            table.items[r] = table.items[i];
            table.items[i] = old_item ;
}


 pub fn main() !void {

	// folder my program
	const path = try std.fs.realpathAlloc(myallocator, ".");
    defer myallocator.free(path);

	// query *all folder this program
	var iterdir= try std.fs.cwd().openDir(path,.{ .iterate = true}) ;
    defer iterdir.close();

    var iterabledir = iterdir.iterate();// list file 

	var list_Query = std.ArrayList(Data).init(myallocator);
	defer list_Query.clearAndFree();
	
	var list_Work = std.ArrayList(Data).init(myallocator);
	
	while (try iterabledir.next()) |e| {
			switch(e.kind) {
		       .directory  => { 
			       var new_buf = std.fmt.allocPrint(myallocator,"{s}",.{e.name}) catch unreachable;
			       try	list_Query.append(Data{.dta = new_buf[0..] }) ;
			       std.debug.print("{s}: {s}\r\n", .{@tagName(e.kind), e.name});
			    },
				else => {},
			}
       	}

	
	var n:usize = list_Query.items.len; 
	while ( n > 0) {
		var i:usize = 1;
		var r:usize = 0;
		if ( n > 1 ){
			while (n > 1) : (i +=1) { 
				if (cmpByData(list_Query.items[r].dta, list_Query.items[i].dta)) {
					 swapData(list_Query, r,i   );
				}
				if (i == n - 1 ) { 
					try list_Work.append(Data{.dta =list_Query.items[r].dta}) ;
                    _=list_Query.orderedRemove(0);
                    n = list_Query.items.len;
                    r = 0; i = 0; 
				}
			}
		}
		else {
		        try list_Work.append(Data{.dta =list_Query.items[r].dta}) ;
		        _=list_Query.orderedRemove(r);
		        break ; 
			}
	} // end list sortc
	
	for (list_Work.items) |f| {
		std.debug.print("list {s}\r\n", .{f.dta});
	}
}

I didn’t use a complex or sophisticated sorting method because I don’t think it’s necessary for this type of search. We’re dealing with main directories, and once one is selected, you can start another search, and so on.