So I want to sort a fruit list alphabetically:
const allocator = std.heap.page_allocator;
var list = std.ArrayList([]const u8).init(allocator);
defer list.deinit();
try list.append("banana");
try list.append("apple");
try list.append("cherry");
// Sort the ArrayList alphabetically using std.mem.sort with the custom comparator
std.mem.sort([]const u8, list.items, ??, ??);
for (list.items) |item| {
std.debug.print("{s}n", .{item});
}
I’ve found this std.mem.sort function in standard lib. Not sure this is the better approach for sorting lists.
Of course I can implement the algorithm from zero but I’m wondering if it’s currently possible to make it done in a more “ziggy” way. I’ve read the std lib is not well written but I want to give it a chance.
And regarding why am I allocating heap memory instead of just pushing fruits onto the stack? I’m open to use the stack, my only interest is about sorting an array of strings
1